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> |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 9 | #include <base/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 | |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 36 | const string Hardware::BootKernelDevice() { |
| 37 | return utils::KernelDeviceOfBootDevice(Hardware::BootDevice()); |
| 38 | } |
| 39 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 40 | const string Hardware::BootDevice() { |
| 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, |
| 58 | bool* bootable) { |
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 | |
| 79 | bool Hardware::MarkKernelUnbootable(const std::string& kernel_device) { |
| 80 | LOG(INFO) << "MarkPartitionUnbootable: " << kernel_device; |
| 81 | |
Don Garrett | 6646b44 | 2013-11-13 15:29:11 -0800 | [diff] [blame] | 82 | if (kernel_device == BootKernelDevice()) { |
| 83 | LOG(ERROR) << "Refusing to mark current kernel as unbootable."; |
| 84 | return false; |
| 85 | } |
| 86 | |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame^] | 87 | std::string disk_name; |
| 88 | int partition_num = 0; |
| 89 | |
| 90 | if (!utils::SplitPartitionName(kernel_device, &disk_name, &partition_num)) |
| 91 | return false; |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 92 | |
| 93 | CgptAddParams params; |
| 94 | memset(¶ms, 0, sizeof(params)); |
| 95 | |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame^] | 96 | params.drive_name = const_cast<char *>(disk_name.c_str()); |
| 97 | params.partition = partition_num; |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 98 | |
| 99 | params.successful = false; |
| 100 | params.set_successful = true; |
| 101 | |
| 102 | params.tries = 0; |
| 103 | params.set_tries = true; |
| 104 | |
| 105 | int retval = CgptSetAttributes(¶ms); |
| 106 | if (retval != CGPT_OK) { |
| 107 | LOG(ERROR) << "Marking kernel unbootable failed."; |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 114 | bool Hardware::IsOfficialBuild() { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 115 | return VbGetSystemPropertyInt("debug_build") == 0; |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | bool Hardware::IsNormalBootMode() { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 119 | bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0; |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 120 | LOG_IF(INFO, dev_mode) << "Booted in dev mode."; |
| 121 | return !dev_mode; |
| 122 | } |
| 123 | |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 124 | static string ReadValueFromCrosSystem(const string& key) { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 125 | char value_buffer[VB_MAX_STRING_PROPERTY]; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 126 | |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 127 | const char *rv = VbGetSystemPropertyString(key.c_str(), value_buffer, |
| 128 | sizeof(value_buffer)); |
| 129 | if (rv != NULL) { |
| 130 | string return_value(value_buffer); |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 131 | TrimWhitespaceASCII(return_value, TRIM_ALL, &return_value); |
| 132 | return return_value; |
| 133 | } |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 134 | |
| 135 | LOG(ERROR) << "Unable to read crossystem key " << key; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 136 | return ""; |
| 137 | } |
| 138 | |
| 139 | string Hardware::GetHardwareClass() { |
Chris Masone | f8d037f | 2014-02-19 01:53:00 +0000 | [diff] [blame] | 140 | if (USE_HWID_OVERRIDE) { |
| 141 | return HwidOverride::Read(base::FilePath("/")); |
| 142 | } |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 143 | return ReadValueFromCrosSystem("hwid"); |
| 144 | } |
| 145 | |
| 146 | string Hardware::GetFirmwareVersion() { |
| 147 | return ReadValueFromCrosSystem("fwid"); |
| 148 | } |
| 149 | |
| 150 | string Hardware::GetECVersion() { |
| 151 | string input_line; |
| 152 | int exit_code = 0; |
| 153 | vector<string> cmd(1, "/usr/sbin/mosys"); |
| 154 | cmd.push_back("-k"); |
| 155 | cmd.push_back("ec"); |
| 156 | cmd.push_back("info"); |
| 157 | |
| 158 | bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line); |
| 159 | if (!success || exit_code) { |
| 160 | LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")"; |
| 161 | return ""; |
| 162 | } |
| 163 | |
| 164 | return utils::ParseECVersion(input_line); |
| 165 | } |
| 166 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 167 | } // namespace chromeos_update_engine |