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 | |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 13 | #include "update_engine/subprocess.h" |
| 14 | #include "update_engine/utils.h" |
| 15 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 16 | using std::string; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 17 | using std::vector; |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 18 | |
| 19 | namespace chromeos_update_engine { |
| 20 | |
| 21 | const string Hardware::BootDevice() { |
| 22 | char boot_path[PATH_MAX]; |
| 23 | // Resolve the boot device path fully, including dereferencing |
| 24 | // through dm-verity. |
| 25 | int ret = rootdev(boot_path, sizeof(boot_path), true, false); |
| 26 | |
| 27 | if (ret < 0) { |
| 28 | LOG(ERROR) << "rootdev failed to find the root device"; |
| 29 | return ""; |
| 30 | } |
| 31 | LOG_IF(WARNING, ret > 0) << "rootdev found a device name with no device node"; |
| 32 | |
| 33 | // This local variable is used to construct the return string and is not |
| 34 | // passed around after use. |
| 35 | return boot_path; |
| 36 | } |
| 37 | |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 38 | bool Hardware::IsOfficialBuild() { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame^] | 39 | return VbGetSystemPropertyInt("debug_build") == 0; |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | bool Hardware::IsNormalBootMode() { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame^] | 43 | bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0; |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 44 | LOG_IF(INFO, dev_mode) << "Booted in dev mode."; |
| 45 | return !dev_mode; |
| 46 | } |
| 47 | |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 48 | static string ReadValueFromCrosSystem(const string& key) { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame^] | 49 | char value_buffer[VB_MAX_STRING_PROPERTY]; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 50 | |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame^] | 51 | const char *rv = VbGetSystemPropertyString(key.c_str(), value_buffer, |
| 52 | sizeof(value_buffer)); |
| 53 | if (rv != NULL) { |
| 54 | string return_value(value_buffer); |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 55 | TrimWhitespaceASCII(return_value, TRIM_ALL, &return_value); |
| 56 | return return_value; |
| 57 | } |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame^] | 58 | |
| 59 | LOG(ERROR) << "Unable to read crossystem key " << key; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 60 | return ""; |
| 61 | } |
| 62 | |
| 63 | string Hardware::GetHardwareClass() { |
| 64 | return ReadValueFromCrosSystem("hwid"); |
| 65 | } |
| 66 | |
| 67 | string Hardware::GetFirmwareVersion() { |
| 68 | return ReadValueFromCrosSystem("fwid"); |
| 69 | } |
| 70 | |
| 71 | string Hardware::GetECVersion() { |
| 72 | string input_line; |
| 73 | int exit_code = 0; |
| 74 | vector<string> cmd(1, "/usr/sbin/mosys"); |
| 75 | cmd.push_back("-k"); |
| 76 | cmd.push_back("ec"); |
| 77 | cmd.push_back("info"); |
| 78 | |
| 79 | bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line); |
| 80 | if (!success || exit_code) { |
| 81 | LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")"; |
| 82 | return ""; |
| 83 | } |
| 84 | |
| 85 | return utils::ParseECVersion(input_line); |
| 86 | } |
| 87 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 88 | } // namespace chromeos_update_engine |