blob: a44edcf755dfee1758ad07880d64ad9819594371 [file] [log] [blame]
Alex Deymo42432912013-07-12 20:21:15 -07001// 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
7#include <base/logging.h>
J. Richard Barnette522d36f2013-10-28 17:22:12 -07008#include <base/string_util.h>
Alex Deymo42432912013-07-12 20:21:15 -07009#include <rootdev/rootdev.h>
10
J. Richard Barnette522d36f2013-10-28 17:22:12 -070011#include "update_engine/subprocess.h"
12#include "update_engine/utils.h"
13
Alex Deymo42432912013-07-12 20:21:15 -070014using std::string;
J. Richard Barnette522d36f2013-10-28 17:22:12 -070015using std::vector;
Alex Deymo42432912013-07-12 20:21:15 -070016
17namespace chromeos_update_engine {
18
19const string Hardware::BootDevice() {
20 char boot_path[PATH_MAX];
21 // Resolve the boot device path fully, including dereferencing
22 // through dm-verity.
23 int ret = rootdev(boot_path, sizeof(boot_path), true, false);
24
25 if (ret < 0) {
26 LOG(ERROR) << "rootdev failed to find the root device";
27 return "";
28 }
29 LOG_IF(WARNING, ret > 0) << "rootdev found a device name with no device node";
30
31 // This local variable is used to construct the return string and is not
32 // passed around after use.
33 return boot_path;
34}
35
J. Richard Barnette522d36f2013-10-28 17:22:12 -070036static string ReadValueFromCrosSystem(const string& key) {
37 int exit_code = 0;
38 vector<string> cmd(1, "/usr/bin/crossystem");
39 cmd.push_back(key);
40
41 string return_value;
42 bool success = Subprocess::SynchronousExec(cmd, &exit_code, &return_value);
43 if (success && !exit_code) {
44 TrimWhitespaceASCII(return_value, TRIM_ALL, &return_value);
45 return return_value;
46 }
47 LOG(ERROR) << "Unable to read " << key << " (" << exit_code << ") "
48 << return_value;
49 return "";
50}
51
52string Hardware::GetHardwareClass() {
53 return ReadValueFromCrosSystem("hwid");
54}
55
56string Hardware::GetFirmwareVersion() {
57 return ReadValueFromCrosSystem("fwid");
58}
59
60string Hardware::GetECVersion() {
61 string input_line;
62 int exit_code = 0;
63 vector<string> cmd(1, "/usr/sbin/mosys");
64 cmd.push_back("-k");
65 cmd.push_back("ec");
66 cmd.push_back("info");
67
68 bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line);
69 if (!success || exit_code) {
70 LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")";
71 return "";
72 }
73
74 return utils::ParseECVersion(input_line);
75}
76
Alex Deymo42432912013-07-12 20:21:15 -070077} // namespace chromeos_update_engine