Alex Deymo | 763e7db | 2015-08-27 21:08:08 -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_chromeos.h" |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 18 | |
Yifan Hong | c96589a | 2018-09-24 16:53:24 -0700 | [diff] [blame] | 19 | #include <memory> |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 20 | #include <string> |
Yifan Hong | c96589a | 2018-09-24 16:53:24 -0700 | [diff] [blame] | 21 | #include <utility> |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 22 | |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 23 | #include <base/bind.h> |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 24 | #include <base/files/file_path.h> |
| 25 | #include <base/files/file_util.h> |
| 26 | #include <base/strings/string_util.h> |
| 27 | #include <rootdev/rootdev.h> |
| 28 | |
| 29 | extern "C" { |
| 30 | #include <vboot/vboot_host.h> |
| 31 | } |
| 32 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 33 | #include "update_engine/common/boot_control.h" |
| 34 | #include "update_engine/common/subprocess.h" |
| 35 | #include "update_engine/common/utils.h" |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 36 | |
| 37 | using std::string; |
| 38 | |
| 39 | namespace { |
| 40 | |
| 41 | const char* kChromeOSPartitionNameKernel = "kernel"; |
| 42 | const char* kChromeOSPartitionNameRoot = "root"; |
| 43 | const char* kAndroidPartitionNameKernel = "boot"; |
| 44 | const char* kAndroidPartitionNameRoot = "system"; |
| 45 | |
Xiaochu Liu | 0d69220 | 2018-10-18 10:52:20 -0700 | [diff] [blame] | 46 | const char kDlcInstallRootDirectoryEncrypted[] = "/home/chronos/dlc"; |
| 47 | const char kPartitionNamePrefixDlc[] = "dlc_"; |
| 48 | const char kPartitionNameDlcA[] = "dlc_a"; |
| 49 | const char kPartitionNameDlcB[] = "dlc_b"; |
| 50 | const char kPartitionNameDlcImage[] = "dlc.img"; |
| 51 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 52 | // Returns the currently booted rootfs partition. "/dev/sda3", for example. |
| 53 | string GetBootDevice() { |
| 54 | char boot_path[PATH_MAX]; |
| 55 | // Resolve the boot device path fully, including dereferencing through |
| 56 | // dm-verity. |
| 57 | int ret = rootdev(boot_path, sizeof(boot_path), true, false); |
| 58 | if (ret < 0) { |
| 59 | LOG(ERROR) << "rootdev failed to find the root device"; |
| 60 | return ""; |
| 61 | } |
| 62 | LOG_IF(WARNING, ret > 0) << "rootdev found a device name with no device node"; |
| 63 | |
| 64 | // This local variable is used to construct the return string and is not |
| 65 | // passed around after use. |
| 66 | return boot_path; |
| 67 | } |
| 68 | |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 69 | // ExecCallback called when the execution of setgoodkernel finishes. Notifies |
| 70 | // the caller of MarkBootSuccessfullAsync() by calling |callback| with the |
| 71 | // result. |
| 72 | void OnMarkBootSuccessfulDone(base::Callback<void(bool)> callback, |
| 73 | int return_code, |
| 74 | const string& output) { |
| 75 | callback.Run(return_code == 0); |
| 76 | } |
| 77 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 78 | } // namespace |
| 79 | |
| 80 | namespace chromeos_update_engine { |
| 81 | |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 82 | namespace boot_control { |
| 83 | |
| 84 | // Factory defined in boot_control.h. |
| 85 | std::unique_ptr<BootControlInterface> CreateBootControl() { |
| 86 | std::unique_ptr<BootControlChromeOS> boot_control_chromeos( |
| 87 | new BootControlChromeOS()); |
| 88 | if (!boot_control_chromeos->Init()) { |
| 89 | LOG(ERROR) << "Ignoring BootControlChromeOS failure. We won't run updates."; |
| 90 | } |
Alex Vakulenko | ce8c8ee | 2016-04-08 08:59:26 -0700 | [diff] [blame] | 91 | return std::move(boot_control_chromeos); |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | } // namespace boot_control |
| 95 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 96 | bool BootControlChromeOS::Init() { |
| 97 | string boot_device = GetBootDevice(); |
| 98 | if (boot_device.empty()) |
| 99 | return false; |
| 100 | |
| 101 | int partition_num; |
| 102 | if (!utils::SplitPartitionName(boot_device, &boot_disk_name_, &partition_num)) |
| 103 | return false; |
| 104 | |
| 105 | // All installed Chrome OS devices have two slots. We don't update removable |
| 106 | // devices, so we will pretend we have only one slot in that case. |
| 107 | if (IsRemovableDevice(boot_disk_name_)) { |
| 108 | LOG(INFO) |
| 109 | << "Booted from a removable device, pretending we have only one slot."; |
| 110 | num_slots_ = 1; |
| 111 | } else { |
| 112 | // TODO(deymo): Look at the actual number of slots reported in the GPT. |
| 113 | num_slots_ = 2; |
| 114 | } |
| 115 | |
| 116 | // Search through the slots to see which slot has the partition_num we booted |
| 117 | // from. This should map to one of the existing slots, otherwise something is |
| 118 | // very wrong. |
| 119 | current_slot_ = 0; |
| 120 | while (current_slot_ < num_slots_ && |
| 121 | partition_num != |
| 122 | GetPartitionNumber(kChromeOSPartitionNameRoot, current_slot_)) { |
| 123 | current_slot_++; |
| 124 | } |
| 125 | if (current_slot_ >= num_slots_) { |
| 126 | LOG(ERROR) << "Couldn't find the slot number corresponding to the " |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame^] | 127 | << "partition " << boot_device << ", number of slots: " |
| 128 | << num_slots_ << ". This device is not updateable."; |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 129 | num_slots_ = 1; |
| 130 | current_slot_ = BootControlInterface::kInvalidSlot; |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | LOG(INFO) << "Booted from slot " << current_slot_ << " (slot " |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 135 | << SlotName(current_slot_) << ") of " << num_slots_ |
| 136 | << " slots present on disk " << boot_disk_name_; |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 137 | return true; |
| 138 | } |
| 139 | |
| 140 | unsigned int BootControlChromeOS::GetNumSlots() const { |
| 141 | return num_slots_; |
| 142 | } |
| 143 | |
| 144 | BootControlInterface::Slot BootControlChromeOS::GetCurrentSlot() const { |
| 145 | return current_slot_; |
| 146 | } |
| 147 | |
| 148 | bool BootControlChromeOS::GetPartitionDevice(const string& partition_name, |
| 149 | unsigned int slot, |
| 150 | string* device) const { |
Xiaochu Liu | f53a5d3 | 2018-11-26 13:48:59 -0800 | [diff] [blame] | 151 | // Partition name prefixed with |kPartitionNamePrefixDlc| is a DLC module. |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 152 | if (base::StartsWith(partition_name, |
Xiaochu Liu | 0d69220 | 2018-10-18 10:52:20 -0700 | [diff] [blame] | 153 | kPartitionNamePrefixDlc, |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 154 | base::CompareCase::SENSITIVE)) { |
Xiaochu Liu | f53a5d3 | 2018-11-26 13:48:59 -0800 | [diff] [blame] | 155 | // Extract DLC module ID from partition_name (DLC module ID is the string |
| 156 | // after |kPartitionNamePrefixDlc| in partition_name). |
| 157 | const auto dlc_module_id = |
| 158 | partition_name.substr(strlen(kPartitionNamePrefixDlc)); |
| 159 | if (dlc_module_id.empty()) { |
| 160 | LOG(ERROR) << " partition name does not contain DLC module ID:" |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 161 | << partition_name; |
| 162 | return false; |
| 163 | } |
Xiaochu Liu | 0d69220 | 2018-10-18 10:52:20 -0700 | [diff] [blame] | 164 | *device = base::FilePath(kDlcInstallRootDirectoryEncrypted) |
Xiaochu Liu | f53a5d3 | 2018-11-26 13:48:59 -0800 | [diff] [blame] | 165 | .Append(dlc_module_id) |
Xiaochu Liu | 0d69220 | 2018-10-18 10:52:20 -0700 | [diff] [blame] | 166 | .Append(slot == 0 ? kPartitionNameDlcA : kPartitionNameDlcB) |
| 167 | .Append(kPartitionNameDlcImage) |
| 168 | .value(); |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 169 | return true; |
| 170 | } |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 171 | int partition_num = GetPartitionNumber(partition_name, slot); |
| 172 | if (partition_num < 0) |
| 173 | return false; |
| 174 | |
| 175 | string part_device = utils::MakePartitionName(boot_disk_name_, partition_num); |
| 176 | if (part_device.empty()) |
| 177 | return false; |
| 178 | |
| 179 | *device = part_device; |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | bool BootControlChromeOS::IsSlotBootable(Slot slot) const { |
| 184 | int partition_num = GetPartitionNumber(kChromeOSPartitionNameKernel, slot); |
| 185 | if (partition_num < 0) |
| 186 | return false; |
| 187 | |
| 188 | CgptAddParams params; |
| 189 | memset(¶ms, '\0', sizeof(params)); |
| 190 | params.drive_name = const_cast<char*>(boot_disk_name_.c_str()); |
| 191 | params.partition = partition_num; |
| 192 | |
| 193 | int retval = CgptGetPartitionDetails(¶ms); |
| 194 | if (retval != CGPT_OK) |
| 195 | return false; |
| 196 | |
| 197 | return params.successful || params.tries > 0; |
| 198 | } |
| 199 | |
| 200 | bool BootControlChromeOS::MarkSlotUnbootable(Slot slot) { |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 201 | LOG(INFO) << "Marking slot " << SlotName(slot) << " unbootable"; |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 202 | |
| 203 | if (slot == current_slot_) { |
| 204 | LOG(ERROR) << "Refusing to mark current slot as unbootable."; |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | int partition_num = GetPartitionNumber(kChromeOSPartitionNameKernel, slot); |
| 209 | if (partition_num < 0) |
| 210 | return false; |
| 211 | |
| 212 | CgptAddParams params; |
| 213 | memset(¶ms, 0, sizeof(params)); |
| 214 | |
| 215 | params.drive_name = const_cast<char*>(boot_disk_name_.c_str()); |
| 216 | params.partition = partition_num; |
| 217 | |
| 218 | params.successful = false; |
| 219 | params.set_successful = true; |
| 220 | |
| 221 | params.tries = 0; |
| 222 | params.set_tries = true; |
| 223 | |
| 224 | int retval = CgptSetAttributes(¶ms); |
| 225 | if (retval != CGPT_OK) { |
| 226 | LOG(ERROR) << "Marking kernel unbootable failed."; |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | return true; |
| 231 | } |
| 232 | |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 233 | bool BootControlChromeOS::SetActiveBootSlot(Slot slot) { |
| 234 | LOG(INFO) << "Marking slot " << SlotName(slot) << " active."; |
| 235 | |
| 236 | int partition_num = GetPartitionNumber(kChromeOSPartitionNameKernel, slot); |
| 237 | if (partition_num < 0) |
| 238 | return false; |
| 239 | |
| 240 | CgptPrioritizeParams prio_params; |
| 241 | memset(&prio_params, 0, sizeof(prio_params)); |
| 242 | |
| 243 | prio_params.drive_name = const_cast<char*>(boot_disk_name_.c_str()); |
| 244 | prio_params.set_partition = partition_num; |
| 245 | |
| 246 | prio_params.max_priority = 0; |
| 247 | |
| 248 | int retval = CgptPrioritize(&prio_params); |
| 249 | if (retval != CGPT_OK) { |
| 250 | LOG(ERROR) << "Unable to set highest priority for slot " << SlotName(slot) |
| 251 | << " (partition " << partition_num << ")."; |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | CgptAddParams add_params; |
| 256 | memset(&add_params, 0, sizeof(add_params)); |
| 257 | |
| 258 | add_params.drive_name = const_cast<char*>(boot_disk_name_.c_str()); |
| 259 | add_params.partition = partition_num; |
| 260 | |
| 261 | add_params.tries = 6; |
| 262 | add_params.set_tries = true; |
| 263 | |
| 264 | retval = CgptSetAttributes(&add_params); |
| 265 | if (retval != CGPT_OK) { |
| 266 | LOG(ERROR) << "Unable to set NumTriesLeft to " << add_params.tries |
| 267 | << " for slot " << SlotName(slot) << " (partition " |
| 268 | << partition_num << ")."; |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | return true; |
| 273 | } |
| 274 | |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 275 | bool BootControlChromeOS::MarkBootSuccessfulAsync( |
| 276 | base::Callback<void(bool)> callback) { |
| 277 | return Subprocess::Get().Exec( |
| 278 | {"/usr/sbin/chromeos-setgoodkernel"}, |
| 279 | base::Bind(&OnMarkBootSuccessfulDone, callback)) != 0; |
| 280 | } |
| 281 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 282 | // static |
| 283 | string BootControlChromeOS::SysfsBlockDevice(const string& device) { |
| 284 | base::FilePath device_path(device); |
| 285 | if (device_path.DirName().value() != "/dev") { |
| 286 | return ""; |
| 287 | } |
| 288 | return base::FilePath("/sys/block").Append(device_path.BaseName()).value(); |
| 289 | } |
| 290 | |
| 291 | // static |
| 292 | bool BootControlChromeOS::IsRemovableDevice(const string& device) { |
| 293 | string sysfs_block = SysfsBlockDevice(device); |
| 294 | string removable; |
| 295 | if (sysfs_block.empty() || |
| 296 | !base::ReadFileToString(base::FilePath(sysfs_block).Append("removable"), |
| 297 | &removable)) { |
| 298 | return false; |
| 299 | } |
| 300 | base::TrimWhitespaceASCII(removable, base::TRIM_ALL, &removable); |
| 301 | return removable == "1"; |
| 302 | } |
| 303 | |
| 304 | int BootControlChromeOS::GetPartitionNumber( |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame^] | 305 | const string partition_name, BootControlInterface::Slot slot) const { |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 306 | if (slot >= num_slots_) { |
| 307 | LOG(ERROR) << "Invalid slot number: " << slot << ", we only have " |
| 308 | << num_slots_ << " slot(s)"; |
| 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | // In Chrome OS, the partition numbers are hard-coded: |
| 313 | // KERNEL-A=2, ROOT-A=3, KERNEL-B=4, ROOT-B=4, ... |
| 314 | // To help compatibility between different we accept both lowercase and |
| 315 | // uppercase names in the ChromeOS or Brillo standard names. |
| 316 | // See http://www.chromium.org/chromium-os/chromiumos-design-docs/disk-format |
Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 317 | string partition_lower = base::ToLowerASCII(partition_name); |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 318 | int base_part_num = 2 + 2 * slot; |
| 319 | if (partition_lower == kChromeOSPartitionNameKernel || |
| 320 | partition_lower == kAndroidPartitionNameKernel) |
| 321 | return base_part_num + 0; |
| 322 | if (partition_lower == kChromeOSPartitionNameRoot || |
| 323 | partition_lower == kAndroidPartitionNameRoot) |
| 324 | return base_part_num + 1; |
| 325 | LOG(ERROR) << "Unknown Chrome OS partition name \"" << partition_name << "\""; |
| 326 | return -1; |
| 327 | } |
| 328 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 329 | bool BootControlChromeOS::InitPartitionMetadata( |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 330 | Slot slot, |
| 331 | const PartitionMetadata& partition_metadata, |
| 332 | bool update_metadata) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 333 | return true; |
| 334 | } |
| 335 | |
| 336 | void BootControlChromeOS::Cleanup() {} |
| 337 | |
Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 338 | } // namespace chromeos_update_engine |