Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "update_engine/hardware.h" |
| 6 | |
Ben Chan | 06c76a4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 7 | #include <base/files/file_util.h> |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 8 | #include <base/logging.h> |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame^] | 9 | #include <base/strings/string_number_conversions.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 10 | #include <base/strings/string_util.h> |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 11 | #include <rootdev/rootdev.h> |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 12 | #include <vboot/crossystem.h> |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 13 | |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 14 | extern "C" { |
| 15 | #include "vboot/vboot_host.h" |
| 16 | } |
| 17 | |
Chris Masone | f8d037f | 2014-02-19 01:53:00 +0000 | [diff] [blame] | 18 | #include "update_engine/hwid_override.h" |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 19 | #include "update_engine/subprocess.h" |
| 20 | #include "update_engine/utils.h" |
| 21 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 22 | using std::string; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 23 | using std::vector; |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 24 | |
Alex Deymo | bccbc38 | 2014-04-03 13:38:55 -0700 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | static const char kOOBECompletedMarker[] = "/home/chronos/.oobe_completed"; |
| 28 | |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame^] | 29 | // The powerwash_count marker file contains the number of times the device was |
| 30 | // powerwashed. This value is incremented by the clobber-state script when |
| 31 | // a powerwash is performed. |
| 32 | static const char kPowerwashCountMarker[] = |
| 33 | "/mnt/stateful_partition/unencrypted/preserve/powerwash_count"; |
| 34 | |
Alex Deymo | bccbc38 | 2014-04-03 13:38:55 -0700 | [diff] [blame] | 35 | } // namespace |
| 36 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 37 | namespace chromeos_update_engine { |
| 38 | |
Chris Masone | f8d037f | 2014-02-19 01:53:00 +0000 | [diff] [blame] | 39 | Hardware::Hardware() {} |
| 40 | |
| 41 | Hardware::~Hardware() {} |
| 42 | |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 43 | string Hardware::BootKernelDevice() const { |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 44 | return utils::KernelDeviceOfBootDevice(Hardware::BootDevice()); |
| 45 | } |
| 46 | |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 47 | string Hardware::BootDevice() const { |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 48 | char boot_path[PATH_MAX]; |
| 49 | // Resolve the boot device path fully, including dereferencing |
| 50 | // through dm-verity. |
| 51 | int ret = rootdev(boot_path, sizeof(boot_path), true, false); |
| 52 | |
| 53 | if (ret < 0) { |
| 54 | LOG(ERROR) << "rootdev failed to find the root device"; |
| 55 | return ""; |
| 56 | } |
| 57 | LOG_IF(WARNING, ret > 0) << "rootdev found a device name with no device node"; |
| 58 | |
| 59 | // This local variable is used to construct the return string and is not |
| 60 | // passed around after use. |
| 61 | return boot_path; |
| 62 | } |
| 63 | |
Alex Deymo | 5708ecd | 2014-04-29 19:44:50 -0700 | [diff] [blame] | 64 | bool Hardware::IsBootDeviceRemovable() const { |
| 65 | return utils::IsRemovableDevice(utils::GetDiskName(BootDevice())); |
| 66 | } |
| 67 | |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 68 | bool Hardware::IsKernelBootable(const std::string& kernel_device, |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 69 | bool* bootable) const { |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 70 | CgptAddParams params; |
| 71 | memset(¶ms, '\0', sizeof(params)); |
| 72 | |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 73 | std::string disk_name; |
| 74 | int partition_num = 0; |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 75 | |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 76 | if (!utils::SplitPartitionName(kernel_device, &disk_name, &partition_num)) |
| 77 | return false; |
| 78 | |
| 79 | params.drive_name = const_cast<char *>(disk_name.c_str()); |
| 80 | params.partition = partition_num; |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 81 | |
| 82 | int retval = CgptGetPartitionDetails(¶ms); |
| 83 | if (retval != CGPT_OK) |
| 84 | return false; |
| 85 | |
| 86 | *bootable = params.successful || (params.tries > 0); |
| 87 | return true; |
| 88 | } |
| 89 | |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 90 | std::vector<std::string> Hardware::GetKernelDevices() const { |
Alex Vakulenko | 59e253e | 2014-02-24 10:40:21 -0800 | [diff] [blame] | 91 | LOG(INFO) << "GetAllKernelDevices"; |
| 92 | |
| 93 | std::string disk_name = utils::GetDiskName(Hardware::BootKernelDevice()); |
Alex Deymo | df632d1 | 2014-04-29 20:04:36 -0700 | [diff] [blame] | 94 | if (disk_name.empty()) { |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 95 | LOG(ERROR) << "Failed to get the current kernel boot disk name"; |
Alex Vakulenko | 59e253e | 2014-02-24 10:40:21 -0800 | [diff] [blame] | 96 | return std::vector<std::string>(); |
| 97 | } |
| 98 | |
| 99 | std::vector<std::string> devices; |
Alex Deymo | df632d1 | 2014-04-29 20:04:36 -0700 | [diff] [blame] | 100 | for (int partition_num : {2, 4}) { // for now, only #2, #4 for slot A & B |
Alex Vakulenko | 59e253e | 2014-02-24 10:40:21 -0800 | [diff] [blame] | 101 | std::string device = utils::MakePartitionName(disk_name, partition_num); |
Alex Deymo | df632d1 | 2014-04-29 20:04:36 -0700 | [diff] [blame] | 102 | if (!device.empty()) { |
Alex Vakulenko | 59e253e | 2014-02-24 10:40:21 -0800 | [diff] [blame] | 103 | devices.push_back(std::move(device)); |
| 104 | } else { |
| 105 | LOG(ERROR) << "Cannot make a partition name for disk: " |
| 106 | << disk_name << ", partition: " << partition_num; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return devices; |
| 111 | } |
| 112 | |
| 113 | |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 114 | bool Hardware::MarkKernelUnbootable(const std::string& kernel_device) { |
| 115 | LOG(INFO) << "MarkPartitionUnbootable: " << kernel_device; |
| 116 | |
Don Garrett | 6646b44 | 2013-11-13 15:29:11 -0800 | [diff] [blame] | 117 | if (kernel_device == BootKernelDevice()) { |
| 118 | LOG(ERROR) << "Refusing to mark current kernel as unbootable."; |
| 119 | return false; |
| 120 | } |
| 121 | |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 122 | std::string disk_name; |
| 123 | int partition_num = 0; |
| 124 | |
| 125 | if (!utils::SplitPartitionName(kernel_device, &disk_name, &partition_num)) |
| 126 | return false; |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 127 | |
| 128 | CgptAddParams params; |
| 129 | memset(¶ms, 0, sizeof(params)); |
| 130 | |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 131 | params.drive_name = const_cast<char *>(disk_name.c_str()); |
| 132 | params.partition = partition_num; |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 133 | |
| 134 | params.successful = false; |
| 135 | params.set_successful = true; |
| 136 | |
| 137 | params.tries = 0; |
| 138 | params.set_tries = true; |
| 139 | |
| 140 | int retval = CgptSetAttributes(¶ms); |
| 141 | if (retval != CGPT_OK) { |
| 142 | LOG(ERROR) << "Marking kernel unbootable failed."; |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | return true; |
| 147 | } |
| 148 | |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 149 | bool Hardware::IsOfficialBuild() const { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 150 | return VbGetSystemPropertyInt("debug_build") == 0; |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 153 | bool Hardware::IsNormalBootMode() const { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 154 | bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0; |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 155 | LOG_IF(INFO, dev_mode) << "Booted in dev mode."; |
| 156 | return !dev_mode; |
| 157 | } |
| 158 | |
Alex Deymo | bccbc38 | 2014-04-03 13:38:55 -0700 | [diff] [blame] | 159 | bool Hardware::IsOOBEComplete(base::Time* out_time_of_oobe) const { |
| 160 | struct stat statbuf; |
| 161 | if (stat(kOOBECompletedMarker, &statbuf) != 0) { |
| 162 | if (errno != ENOENT) { |
| 163 | PLOG(ERROR) << "Error getting information about " |
| 164 | << kOOBECompletedMarker; |
| 165 | } |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | if (out_time_of_oobe != nullptr) |
| 170 | *out_time_of_oobe = base::Time::FromTimeT(statbuf.st_mtime); |
| 171 | return true; |
| 172 | } |
| 173 | |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 174 | static string ReadValueFromCrosSystem(const string& key) { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 175 | char value_buffer[VB_MAX_STRING_PROPERTY]; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 176 | |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 177 | const char *rv = VbGetSystemPropertyString(key.c_str(), value_buffer, |
| 178 | sizeof(value_buffer)); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 179 | if (rv != nullptr) { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 180 | string return_value(value_buffer); |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 181 | base::TrimWhitespaceASCII(return_value, base::TRIM_ALL, &return_value); |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 182 | return return_value; |
| 183 | } |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 184 | |
| 185 | LOG(ERROR) << "Unable to read crossystem key " << key; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 186 | return ""; |
| 187 | } |
| 188 | |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 189 | string Hardware::GetHardwareClass() const { |
Chris Masone | f8d037f | 2014-02-19 01:53:00 +0000 | [diff] [blame] | 190 | if (USE_HWID_OVERRIDE) { |
| 191 | return HwidOverride::Read(base::FilePath("/")); |
| 192 | } |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 193 | return ReadValueFromCrosSystem("hwid"); |
| 194 | } |
| 195 | |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 196 | string Hardware::GetFirmwareVersion() const { |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 197 | return ReadValueFromCrosSystem("fwid"); |
| 198 | } |
| 199 | |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 200 | string Hardware::GetECVersion() const { |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 201 | string input_line; |
| 202 | int exit_code = 0; |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 203 | vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"}; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 204 | |
| 205 | bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line); |
| 206 | if (!success || exit_code) { |
| 207 | LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")"; |
| 208 | return ""; |
| 209 | } |
| 210 | |
| 211 | return utils::ParseECVersion(input_line); |
| 212 | } |
| 213 | |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame^] | 214 | int Hardware::GetPowerwashCount() const { |
| 215 | int powerwash_count; |
| 216 | string contents; |
| 217 | if (!utils::ReadFile(kPowerwashCountMarker, &contents)) |
| 218 | return -1; |
| 219 | base::TrimWhitespaceASCII(contents, base::TRIM_TRAILING, &contents); |
| 220 | if (!base::StringToInt(contents, &powerwash_count)) |
| 221 | return -1; |
| 222 | return powerwash_count; |
| 223 | } |
| 224 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 225 | } // namespace chromeos_update_engine |