blob: f04c340fe7001e6cca7066f1feb06fad02bf5ed4 [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
J. Richard Barnette056b0ab2013-10-29 15:24:56 -07007#include <base/file_util.h>
Alex Deymo42432912013-07-12 20:21:15 -07008#include <base/logging.h>
J. Richard Barnette522d36f2013-10-28 17:22:12 -07009#include <base/string_util.h>
Alex Deymo42432912013-07-12 20:21:15 -070010#include <rootdev/rootdev.h>
11
J. Richard Barnette522d36f2013-10-28 17:22:12 -070012#include "update_engine/subprocess.h"
13#include "update_engine/utils.h"
14
Alex Deymo42432912013-07-12 20:21:15 -070015using std::string;
J. Richard Barnette522d36f2013-10-28 17:22:12 -070016using std::vector;
Alex Deymo42432912013-07-12 20:21:15 -070017
18namespace chromeos_update_engine {
19
J. Richard Barnette056b0ab2013-10-29 15:24:56 -070020static const char kDevImageMarker[] = "/root/.dev_mode";
21
22
Alex Deymo42432912013-07-12 20:21:15 -070023const 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 Barnette056b0ab2013-10-29 15:24:56 -070040bool Hardware::IsOfficialBuild() {
41 return !file_util::PathExists(FilePath(kDevImageMarker));
42}
43
44bool 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 Barnette522d36f2013-10-28 17:22:12 -070059static 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
75string Hardware::GetHardwareClass() {
76 return ReadValueFromCrosSystem("hwid");
77}
78
79string Hardware::GetFirmwareVersion() {
80 return ReadValueFromCrosSystem("fwid");
81}
82
83string 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 Deymo42432912013-07-12 20:21:15 -0700100} // namespace chromeos_update_engine