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 | |
| 30 | namespace chromeos_update_engine { |
| 31 | |
Chris Masone | f8d037f | 2014-02-19 01:53:00 +0000 | [diff] [blame] | 32 | Hardware::Hardware() {} |
| 33 | |
| 34 | Hardware::~Hardware() {} |
| 35 | |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 36 | string Hardware::BootKernelDevice() const { |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 37 | return utils::KernelDeviceOfBootDevice(Hardware::BootDevice()); |
| 38 | } |
| 39 | |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 40 | string Hardware::BootDevice() const { |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 41 | char boot_path[PATH_MAX]; |
| 42 | // Resolve the boot device path fully, including dereferencing |
| 43 | // through dm-verity. |
| 44 | int ret = rootdev(boot_path, sizeof(boot_path), true, false); |
| 45 | |
| 46 | if (ret < 0) { |
| 47 | LOG(ERROR) << "rootdev failed to find the root device"; |
| 48 | return ""; |
| 49 | } |
| 50 | LOG_IF(WARNING, ret > 0) << "rootdev found a device name with no device node"; |
| 51 | |
| 52 | // This local variable is used to construct the return string and is not |
| 53 | // passed around after use. |
| 54 | return boot_path; |
| 55 | } |
| 56 | |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 57 | bool Hardware::IsKernelBootable(const std::string& kernel_device, |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 58 | bool* bootable) const { |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 59 | CgptAddParams params; |
| 60 | memset(¶ms, '\0', sizeof(params)); |
| 61 | |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 62 | std::string disk_name; |
| 63 | int partition_num = 0; |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 64 | |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 65 | if (!utils::SplitPartitionName(kernel_device, &disk_name, &partition_num)) |
| 66 | return false; |
| 67 | |
| 68 | params.drive_name = const_cast<char *>(disk_name.c_str()); |
| 69 | params.partition = partition_num; |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 70 | |
| 71 | int retval = CgptGetPartitionDetails(¶ms); |
| 72 | if (retval != CGPT_OK) |
| 73 | return false; |
| 74 | |
| 75 | *bootable = params.successful || (params.tries > 0); |
| 76 | return true; |
| 77 | } |
| 78 | |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 79 | std::vector<std::string> Hardware::GetKernelDevices() const { |
Alex Vakulenko | 59e253e | 2014-02-24 10:40:21 -0800 | [diff] [blame] | 80 | LOG(INFO) << "GetAllKernelDevices"; |
| 81 | |
| 82 | std::string disk_name = utils::GetDiskName(Hardware::BootKernelDevice()); |
| 83 | if(disk_name.empty()) { |
| 84 | LOG(ERROR) << "Failed to get the cuurent kernel boot disk name"; |
| 85 | return std::vector<std::string>(); |
| 86 | } |
| 87 | |
| 88 | std::vector<std::string> devices; |
Alex Vakulenko | 2bddadd | 2014-03-27 13:23:46 -0700 | [diff] [blame^] | 89 | 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] | 90 | std::string device = utils::MakePartitionName(disk_name, partition_num); |
| 91 | if(!device.empty()) { |
| 92 | devices.push_back(std::move(device)); |
| 93 | } else { |
| 94 | LOG(ERROR) << "Cannot make a partition name for disk: " |
| 95 | << disk_name << ", partition: " << partition_num; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return devices; |
| 100 | } |
| 101 | |
| 102 | |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 103 | bool Hardware::MarkKernelUnbootable(const std::string& kernel_device) { |
| 104 | LOG(INFO) << "MarkPartitionUnbootable: " << kernel_device; |
| 105 | |
Don Garrett | 6646b44 | 2013-11-13 15:29:11 -0800 | [diff] [blame] | 106 | if (kernel_device == BootKernelDevice()) { |
| 107 | LOG(ERROR) << "Refusing to mark current kernel as unbootable."; |
| 108 | return false; |
| 109 | } |
| 110 | |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 111 | std::string disk_name; |
| 112 | int partition_num = 0; |
| 113 | |
| 114 | if (!utils::SplitPartitionName(kernel_device, &disk_name, &partition_num)) |
| 115 | return false; |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 116 | |
| 117 | CgptAddParams params; |
| 118 | memset(¶ms, 0, sizeof(params)); |
| 119 | |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 120 | params.drive_name = const_cast<char *>(disk_name.c_str()); |
| 121 | params.partition = partition_num; |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 122 | |
| 123 | params.successful = false; |
| 124 | params.set_successful = true; |
| 125 | |
| 126 | params.tries = 0; |
| 127 | params.set_tries = true; |
| 128 | |
| 129 | int retval = CgptSetAttributes(¶ms); |
| 130 | if (retval != CGPT_OK) { |
| 131 | LOG(ERROR) << "Marking kernel unbootable failed."; |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | return true; |
| 136 | } |
| 137 | |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 138 | bool Hardware::IsOfficialBuild() const { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 139 | return VbGetSystemPropertyInt("debug_build") == 0; |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 142 | bool Hardware::IsNormalBootMode() const { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 143 | bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0; |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 144 | LOG_IF(INFO, dev_mode) << "Booted in dev mode."; |
| 145 | return !dev_mode; |
| 146 | } |
| 147 | |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 148 | static string ReadValueFromCrosSystem(const string& key) { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 149 | char value_buffer[VB_MAX_STRING_PROPERTY]; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 150 | |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 151 | const char *rv = VbGetSystemPropertyString(key.c_str(), value_buffer, |
| 152 | sizeof(value_buffer)); |
| 153 | if (rv != NULL) { |
| 154 | string return_value(value_buffer); |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 155 | TrimWhitespaceASCII(return_value, TRIM_ALL, &return_value); |
| 156 | return return_value; |
| 157 | } |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 158 | |
| 159 | LOG(ERROR) << "Unable to read crossystem key " << key; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 160 | return ""; |
| 161 | } |
| 162 | |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 163 | string Hardware::GetHardwareClass() const { |
Chris Masone | f8d037f | 2014-02-19 01:53:00 +0000 | [diff] [blame] | 164 | if (USE_HWID_OVERRIDE) { |
| 165 | return HwidOverride::Read(base::FilePath("/")); |
| 166 | } |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 167 | return ReadValueFromCrosSystem("hwid"); |
| 168 | } |
| 169 | |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 170 | string Hardware::GetFirmwareVersion() const { |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 171 | return ReadValueFromCrosSystem("fwid"); |
| 172 | } |
| 173 | |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 174 | string Hardware::GetECVersion() const { |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 175 | string input_line; |
| 176 | int exit_code = 0; |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 177 | vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"}; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 178 | |
| 179 | bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line); |
| 180 | if (!success || exit_code) { |
| 181 | LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")"; |
| 182 | return ""; |
| 183 | } |
| 184 | |
| 185 | return utils::ParseECVersion(input_line); |
| 186 | } |
| 187 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 188 | } // namespace chromeos_update_engine |