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