blob: d547c2db8a447a410c86155ba6ebfd542d673405 [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
J. Richard Barnette522d36f2013-10-28 17:22:12 -070023#include "update_engine/subprocess.h"
24#include "update_engine/utils.h"
25
Alex Deymo42432912013-07-12 20:21:15 -070026using std::string;
J. Richard Barnette522d36f2013-10-28 17:22:12 -070027using std::vector;
Alex Deymo42432912013-07-12 20:21:15 -070028
29namespace chromeos_update_engine {
30
Don Garrett83692e42013-11-08 10:11:30 -080031const string Hardware::BootKernelDevice() {
32 return utils::KernelDeviceOfBootDevice(Hardware::BootDevice());
33}
34
Alex Deymo42432912013-07-12 20:21:15 -070035const string Hardware::BootDevice() {
36 char boot_path[PATH_MAX];
37 // Resolve the boot device path fully, including dereferencing
38 // through dm-verity.
39 int ret = rootdev(boot_path, sizeof(boot_path), true, false);
40
41 if (ret < 0) {
42 LOG(ERROR) << "rootdev failed to find the root device";
43 return "";
44 }
45 LOG_IF(WARNING, ret > 0) << "rootdev found a device name with no device node";
46
47 // This local variable is used to construct the return string and is not
48 // passed around after use.
49 return boot_path;
50}
51
Don Garrett83692e42013-11-08 10:11:30 -080052bool Hardware::IsKernelBootable(const std::string& kernel_device,
53 bool* bootable) {
54
55 if (kernel_device == BootKernelDevice()) {
56 LOG(ERROR) << "Refusing to mark current kernel as unbootable.";
57 return false;
58 }
59
60 CgptAddParams params;
61 memset(&params, '\0', sizeof(params));
62
63 string root_dev = utils::RootDevice(kernel_device);
64 string partition_number_str = utils::PartitionNumber(kernel_device);
65 uint32_t partition_number = atoi(partition_number_str.c_str());
66
67 params.drive_name = const_cast<char *>(root_dev.c_str());
68 params.partition = partition_number;
69
70 int retval = CgptGetPartitionDetails(&params);
71 if (retval != CGPT_OK)
72 return false;
73
74 *bootable = params.successful || (params.tries > 0);
75 return true;
76}
77
78bool Hardware::MarkKernelUnbootable(const std::string& kernel_device) {
79 LOG(INFO) << "MarkPartitionUnbootable: " << kernel_device;
80
81 string root_dev = utils::RootDevice(kernel_device);
82 string partition_number_str = utils::PartitionNumber(kernel_device);
83 uint32_t partition_number = atoi(partition_number_str.c_str());
84
85 CgptAddParams params;
86 memset(&params, 0, sizeof(params));
87
88 params.drive_name = const_cast<char *>(root_dev.c_str());
89 params.partition = partition_number;
90
91 params.successful = false;
92 params.set_successful = true;
93
94 params.tries = 0;
95 params.set_tries = true;
96
97 int retval = CgptSetAttributes(&params);
98 if (retval != CGPT_OK) {
99 LOG(ERROR) << "Marking kernel unbootable failed.";
100 return false;
101 }
102
103 return true;
104}
105
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700106bool Hardware::IsOfficialBuild() {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700107 return VbGetSystemPropertyInt("debug_build") == 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700108}
109
110bool Hardware::IsNormalBootMode() {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700111 bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700112 LOG_IF(INFO, dev_mode) << "Booted in dev mode.";
113 return !dev_mode;
114}
115
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700116static string ReadValueFromCrosSystem(const string& key) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700117 char value_buffer[VB_MAX_STRING_PROPERTY];
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700118
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700119 const char *rv = VbGetSystemPropertyString(key.c_str(), value_buffer,
120 sizeof(value_buffer));
121 if (rv != NULL) {
122 string return_value(value_buffer);
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700123 TrimWhitespaceASCII(return_value, TRIM_ALL, &return_value);
124 return return_value;
125 }
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700126
127 LOG(ERROR) << "Unable to read crossystem key " << key;
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700128 return "";
129}
130
131string Hardware::GetHardwareClass() {
132 return ReadValueFromCrosSystem("hwid");
133}
134
135string Hardware::GetFirmwareVersion() {
136 return ReadValueFromCrosSystem("fwid");
137}
138
139string Hardware::GetECVersion() {
140 string input_line;
141 int exit_code = 0;
142 vector<string> cmd(1, "/usr/sbin/mosys");
143 cmd.push_back("-k");
144 cmd.push_back("ec");
145 cmd.push_back("info");
146
147 bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line);
148 if (!success || exit_code) {
149 LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")";
150 return "";
151 }
152
153 return utils::ParseECVersion(input_line);
154}
155
Alex Deymo42432912013-07-12 20:21:15 -0700156} // namespace chromeos_update_engine