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