blob: 6ab0f22ee85ada3bbf2a197d4c07ff38c9fec934 [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>
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070011#include <vboot/crossystem.h>
Alex Deymo42432912013-07-12 20:21:15 -070012
J. Richard Barnette522d36f2013-10-28 17:22:12 -070013#include "update_engine/subprocess.h"
14#include "update_engine/utils.h"
15
Alex Deymo42432912013-07-12 20:21:15 -070016using std::string;
J. Richard Barnette522d36f2013-10-28 17:22:12 -070017using std::vector;
Alex Deymo42432912013-07-12 20:21:15 -070018
19namespace chromeos_update_engine {
20
21const 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 Barnette056b0ab2013-10-29 15:24:56 -070038bool Hardware::IsOfficialBuild() {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070039 return VbGetSystemPropertyInt("debug_build") == 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -070040}
41
42bool Hardware::IsNormalBootMode() {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070043 bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -070044 LOG_IF(INFO, dev_mode) << "Booted in dev mode.";
45 return !dev_mode;
46}
47
J. Richard Barnette522d36f2013-10-28 17:22:12 -070048static string ReadValueFromCrosSystem(const string& key) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070049 char value_buffer[VB_MAX_STRING_PROPERTY];
J. Richard Barnette522d36f2013-10-28 17:22:12 -070050
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070051 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 Barnette522d36f2013-10-28 17:22:12 -070055 TrimWhitespaceASCII(return_value, TRIM_ALL, &return_value);
56 return return_value;
57 }
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070058
59 LOG(ERROR) << "Unable to read crossystem key " << key;
J. Richard Barnette522d36f2013-10-28 17:22:12 -070060 return "";
61}
62
63string Hardware::GetHardwareClass() {
64 return ReadValueFromCrosSystem("hwid");
65}
66
67string Hardware::GetFirmwareVersion() {
68 return ReadValueFromCrosSystem("fwid");
69}
70
71string 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 Deymo42432912013-07-12 20:21:15 -070088} // namespace chromeos_update_engine