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