blob: 26435cf27db796f2abbad2e2ddc3897b700a969e [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
62 string root_dev = utils::RootDevice(kernel_device);
63 string partition_number_str = utils::PartitionNumber(kernel_device);
64 uint32_t partition_number = atoi(partition_number_str.c_str());
65
66 params.drive_name = const_cast<char *>(root_dev.c_str());
67 params.partition = partition_number;
68
69 int retval = CgptGetPartitionDetails(&params);
70 if (retval != CGPT_OK)
71 return false;
72
73 *bootable = params.successful || (params.tries > 0);
74 return true;
75}
76
77bool Hardware::MarkKernelUnbootable(const std::string& kernel_device) {
78 LOG(INFO) << "MarkPartitionUnbootable: " << kernel_device;
79
Don Garrett6646b442013-11-13 15:29:11 -080080 if (kernel_device == BootKernelDevice()) {
81 LOG(ERROR) << "Refusing to mark current kernel as unbootable.";
82 return false;
83 }
84
Don Garrett83692e42013-11-08 10:11:30 -080085 string root_dev = utils::RootDevice(kernel_device);
86 string partition_number_str = utils::PartitionNumber(kernel_device);
87 uint32_t partition_number = atoi(partition_number_str.c_str());
88
89 CgptAddParams params;
90 memset(&params, 0, sizeof(params));
91
92 params.drive_name = const_cast<char *>(root_dev.c_str());
93 params.partition = partition_number;
94
95 params.successful = false;
96 params.set_successful = true;
97
98 params.tries = 0;
99 params.set_tries = true;
100
101 int retval = CgptSetAttributes(&params);
102 if (retval != CGPT_OK) {
103 LOG(ERROR) << "Marking kernel unbootable failed.";
104 return false;
105 }
106
107 return true;
108}
109
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700110bool Hardware::IsOfficialBuild() {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700111 return VbGetSystemPropertyInt("debug_build") == 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700112}
113
114bool Hardware::IsNormalBootMode() {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700115 bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700116 LOG_IF(INFO, dev_mode) << "Booted in dev mode.";
117 return !dev_mode;
118}
119
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700120static string ReadValueFromCrosSystem(const string& key) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700121 char value_buffer[VB_MAX_STRING_PROPERTY];
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700122
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700123 const char *rv = VbGetSystemPropertyString(key.c_str(), value_buffer,
124 sizeof(value_buffer));
125 if (rv != NULL) {
126 string return_value(value_buffer);
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700127 TrimWhitespaceASCII(return_value, TRIM_ALL, &return_value);
128 return return_value;
129 }
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700130
131 LOG(ERROR) << "Unable to read crossystem key " << key;
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700132 return "";
133}
134
135string Hardware::GetHardwareClass() {
Chris Masonef8d037f2014-02-19 01:53:00 +0000136 if (USE_HWID_OVERRIDE) {
137 return HwidOverride::Read(base::FilePath("/"));
138 }
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700139 return ReadValueFromCrosSystem("hwid");
140}
141
142string Hardware::GetFirmwareVersion() {
143 return ReadValueFromCrosSystem("fwid");
144}
145
146string Hardware::GetECVersion() {
147 string input_line;
148 int exit_code = 0;
149 vector<string> cmd(1, "/usr/sbin/mosys");
150 cmd.push_back("-k");
151 cmd.push_back("ec");
152 cmd.push_back("info");
153
154 bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line);
155 if (!success || exit_code) {
156 LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")";
157 return "";
158 }
159
160 return utils::ParseECVersion(input_line);
161}
162
Alex Deymo42432912013-07-12 20:21:15 -0700163} // namespace chromeos_update_engine