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