blob: 9f3fe00dfd9bfe0f841f3660495607e6762bcd6b [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
Don Garrett83692e42013-11-08 10:11:30 -080013extern "C" {
14#include "vboot/vboot_host.h"
15}
16
17// We don't use these variables, but libcgpt needs them defined to link.
18// TODO(dgarrett) chromium:318536
19const char* progname = "";
20const char* command = "";
21void (*uuid_generator)(uint8_t* buffer) = NULL;
22
Chris Masonef8d037f2014-02-19 01:53:00 +000023#include "update_engine/hwid_override.h"
J. Richard Barnette522d36f2013-10-28 17:22:12 -070024#include "update_engine/subprocess.h"
25#include "update_engine/utils.h"
26
Alex Deymo42432912013-07-12 20:21:15 -070027using std::string;
J. Richard Barnette522d36f2013-10-28 17:22:12 -070028using std::vector;
Alex Deymo42432912013-07-12 20:21:15 -070029
30namespace chromeos_update_engine {
31
Chris Masonef8d037f2014-02-19 01:53:00 +000032Hardware::Hardware() {}
33
34Hardware::~Hardware() {}
35
Don Garrett83692e42013-11-08 10:11:30 -080036const string Hardware::BootKernelDevice() {
37 return utils::KernelDeviceOfBootDevice(Hardware::BootDevice());
38}
39
Alex Deymo42432912013-07-12 20:21:15 -070040const string Hardware::BootDevice() {
41 char boot_path[PATH_MAX];
42 // Resolve the boot device path fully, including dereferencing
43 // through dm-verity.
44 int ret = rootdev(boot_path, sizeof(boot_path), true, false);
45
46 if (ret < 0) {
47 LOG(ERROR) << "rootdev failed to find the root device";
48 return "";
49 }
50 LOG_IF(WARNING, ret > 0) << "rootdev found a device name with no device node";
51
52 // This local variable is used to construct the return string and is not
53 // passed around after use.
54 return boot_path;
55}
56
Don Garrett83692e42013-11-08 10:11:30 -080057bool Hardware::IsKernelBootable(const std::string& kernel_device,
58 bool* bootable) {
Don Garrett83692e42013-11-08 10:11:30 -080059 CgptAddParams params;
60 memset(&params, '\0', sizeof(params));
61
Alex Vakulenko4f5b1442014-02-21 12:19:44 -080062 std::string disk_name;
63 int partition_num = 0;
Don Garrett83692e42013-11-08 10:11:30 -080064
Alex Vakulenko4f5b1442014-02-21 12:19:44 -080065 if (!utils::SplitPartitionName(kernel_device, &disk_name, &partition_num))
66 return false;
67
68 params.drive_name = const_cast<char *>(disk_name.c_str());
69 params.partition = partition_num;
Don Garrett83692e42013-11-08 10:11:30 -080070
71 int retval = CgptGetPartitionDetails(&params);
72 if (retval != CGPT_OK)
73 return false;
74
75 *bootable = params.successful || (params.tries > 0);
76 return true;
77}
78
79bool Hardware::MarkKernelUnbootable(const std::string& kernel_device) {
80 LOG(INFO) << "MarkPartitionUnbootable: " << kernel_device;
81
Don Garrett6646b442013-11-13 15:29:11 -080082 if (kernel_device == BootKernelDevice()) {
83 LOG(ERROR) << "Refusing to mark current kernel as unbootable.";
84 return false;
85 }
86
Alex Vakulenko4f5b1442014-02-21 12:19:44 -080087 std::string disk_name;
88 int partition_num = 0;
89
90 if (!utils::SplitPartitionName(kernel_device, &disk_name, &partition_num))
91 return false;
Don Garrett83692e42013-11-08 10:11:30 -080092
93 CgptAddParams params;
94 memset(&params, 0, sizeof(params));
95
Alex Vakulenko4f5b1442014-02-21 12:19:44 -080096 params.drive_name = const_cast<char *>(disk_name.c_str());
97 params.partition = partition_num;
Don Garrett83692e42013-11-08 10:11:30 -080098
99 params.successful = false;
100 params.set_successful = true;
101
102 params.tries = 0;
103 params.set_tries = true;
104
105 int retval = CgptSetAttributes(&params);
106 if (retval != CGPT_OK) {
107 LOG(ERROR) << "Marking kernel unbootable failed.";
108 return false;
109 }
110
111 return true;
112}
113
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700114bool Hardware::IsOfficialBuild() {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700115 return VbGetSystemPropertyInt("debug_build") == 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700116}
117
118bool Hardware::IsNormalBootMode() {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700119 bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700120 LOG_IF(INFO, dev_mode) << "Booted in dev mode.";
121 return !dev_mode;
122}
123
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700124static string ReadValueFromCrosSystem(const string& key) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700125 char value_buffer[VB_MAX_STRING_PROPERTY];
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700126
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700127 const char *rv = VbGetSystemPropertyString(key.c_str(), value_buffer,
128 sizeof(value_buffer));
129 if (rv != NULL) {
130 string return_value(value_buffer);
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700131 TrimWhitespaceASCII(return_value, TRIM_ALL, &return_value);
132 return return_value;
133 }
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700134
135 LOG(ERROR) << "Unable to read crossystem key " << key;
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700136 return "";
137}
138
139string Hardware::GetHardwareClass() {
Chris Masonef8d037f2014-02-19 01:53:00 +0000140 if (USE_HWID_OVERRIDE) {
141 return HwidOverride::Read(base::FilePath("/"));
142 }
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700143 return ReadValueFromCrosSystem("hwid");
144}
145
146string Hardware::GetFirmwareVersion() {
147 return ReadValueFromCrosSystem("fwid");
148}
149
150string Hardware::GetECVersion() {
151 string input_line;
152 int exit_code = 0;
153 vector<string> cmd(1, "/usr/sbin/mosys");
154 cmd.push_back("-k");
155 cmd.push_back("ec");
156 cmd.push_back("info");
157
158 bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line);
159 if (!success || exit_code) {
160 LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")";
161 return "";
162 }
163
164 return utils::ParseECVersion(input_line);
165}
166
Alex Deymo42432912013-07-12 20:21:15 -0700167} // namespace chromeos_update_engine