blob: 5d5492f6c489fe4c5646f6a64ef0c2ecb879e049 [file] [log] [blame]
Alex Deymo42432912013-07-13 03:21:151// 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 22:24:567#include <base/file_util.h>
Alex Deymo42432912013-07-13 03:21:158#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 19:36:289#include <base/strings/string_util.h>
Alex Deymo42432912013-07-13 03:21:1510#include <rootdev/rootdev.h>
J. Richard Barnettec7dd8532013-10-29 23:30:4611#include <vboot/crossystem.h>
Alex Deymo42432912013-07-13 03:21:1512
Don Garrett83692e42013-11-08 18:11:3013extern "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:0023#include "update_engine/hwid_override.h"
J. Richard Barnette522d36f2013-10-29 00:22:1224#include "update_engine/subprocess.h"
25#include "update_engine/utils.h"
26
Alex Deymo42432912013-07-13 03:21:1527using std::string;
J. Richard Barnette522d36f2013-10-29 00:22:1228using std::vector;
Alex Deymo42432912013-07-13 03:21:1529
Alex Deymobccbc382014-04-03 20:38:5530namespace {
31
32static const char kOOBECompletedMarker[] = "/home/chronos/.oobe_completed";
33
34} // namespace
35
Alex Deymo42432912013-07-13 03:21:1536namespace chromeos_update_engine {
37
Chris Masonef8d037f2014-02-19 01:53:0038Hardware::Hardware() {}
39
40Hardware::~Hardware() {}
41
Alex Vakulenkod0fdfb32014-02-21 23:26:2642string Hardware::BootKernelDevice() const {
Don Garrett83692e42013-11-08 18:11:3043 return utils::KernelDeviceOfBootDevice(Hardware::BootDevice());
44}
45
Alex Vakulenkod0fdfb32014-02-21 23:26:2646string Hardware::BootDevice() const {
Alex Deymo42432912013-07-13 03:21:1547 char boot_path[PATH_MAX];
48 // Resolve the boot device path fully, including dereferencing
49 // through dm-verity.
50 int ret = rootdev(boot_path, sizeof(boot_path), true, false);
51
52 if (ret < 0) {
53 LOG(ERROR) << "rootdev failed to find the root device";
54 return "";
55 }
56 LOG_IF(WARNING, ret > 0) << "rootdev found a device name with no device node";
57
58 // This local variable is used to construct the return string and is not
59 // passed around after use.
60 return boot_path;
61}
62
Don Garrett83692e42013-11-08 18:11:3063bool Hardware::IsKernelBootable(const std::string& kernel_device,
Alex Vakulenkod0fdfb32014-02-21 23:26:2664 bool* bootable) const {
Don Garrett83692e42013-11-08 18:11:3065 CgptAddParams params;
66 memset(&params, '\0', sizeof(params));
67
Alex Vakulenko4f5b1442014-02-21 20:19:4468 std::string disk_name;
69 int partition_num = 0;
Don Garrett83692e42013-11-08 18:11:3070
Alex Vakulenko4f5b1442014-02-21 20:19:4471 if (!utils::SplitPartitionName(kernel_device, &disk_name, &partition_num))
72 return false;
73
74 params.drive_name = const_cast<char *>(disk_name.c_str());
75 params.partition = partition_num;
Don Garrett83692e42013-11-08 18:11:3076
77 int retval = CgptGetPartitionDetails(&params);
78 if (retval != CGPT_OK)
79 return false;
80
81 *bootable = params.successful || (params.tries > 0);
82 return true;
83}
84
Alex Vakulenkod0fdfb32014-02-21 23:26:2685std::vector<std::string> Hardware::GetKernelDevices() const {
Alex Vakulenko59e253e2014-02-24 18:40:2186 LOG(INFO) << "GetAllKernelDevices";
87
88 std::string disk_name = utils::GetDiskName(Hardware::BootKernelDevice());
89 if(disk_name.empty()) {
90 LOG(ERROR) << "Failed to get the cuurent kernel boot disk name";
91 return std::vector<std::string>();
92 }
93
94 std::vector<std::string> devices;
Alex Vakulenko2bddadd2014-03-27 20:23:4695 for (int partition_num : {2, 4}) { // for now, only #2, #4 for slot A & B
Alex Vakulenko59e253e2014-02-24 18:40:2196 std::string device = utils::MakePartitionName(disk_name, partition_num);
97 if(!device.empty()) {
98 devices.push_back(std::move(device));
99 } else {
100 LOG(ERROR) << "Cannot make a partition name for disk: "
101 << disk_name << ", partition: " << partition_num;
102 }
103 }
104
105 return devices;
106}
107
108
Don Garrett83692e42013-11-08 18:11:30109bool Hardware::MarkKernelUnbootable(const std::string& kernel_device) {
110 LOG(INFO) << "MarkPartitionUnbootable: " << kernel_device;
111
Don Garrett6646b442013-11-13 23:29:11112 if (kernel_device == BootKernelDevice()) {
113 LOG(ERROR) << "Refusing to mark current kernel as unbootable.";
114 return false;
115 }
116
Alex Vakulenko4f5b1442014-02-21 20:19:44117 std::string disk_name;
118 int partition_num = 0;
119
120 if (!utils::SplitPartitionName(kernel_device, &disk_name, &partition_num))
121 return false;
Don Garrett83692e42013-11-08 18:11:30122
123 CgptAddParams params;
124 memset(&params, 0, sizeof(params));
125
Alex Vakulenko4f5b1442014-02-21 20:19:44126 params.drive_name = const_cast<char *>(disk_name.c_str());
127 params.partition = partition_num;
Don Garrett83692e42013-11-08 18:11:30128
129 params.successful = false;
130 params.set_successful = true;
131
132 params.tries = 0;
133 params.set_tries = true;
134
135 int retval = CgptSetAttributes(&params);
136 if (retval != CGPT_OK) {
137 LOG(ERROR) << "Marking kernel unbootable failed.";
138 return false;
139 }
140
141 return true;
142}
143
Alex Vakulenkod0fdfb32014-02-21 23:26:26144bool Hardware::IsOfficialBuild() const {
J. Richard Barnettec7dd8532013-10-29 23:30:46145 return VbGetSystemPropertyInt("debug_build") == 0;
J. Richard Barnette056b0ab2013-10-29 22:24:56146}
147
Alex Vakulenkod0fdfb32014-02-21 23:26:26148bool Hardware::IsNormalBootMode() const {
J. Richard Barnettec7dd8532013-10-29 23:30:46149 bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
J. Richard Barnette056b0ab2013-10-29 22:24:56150 LOG_IF(INFO, dev_mode) << "Booted in dev mode.";
151 return !dev_mode;
152}
153
Alex Deymobccbc382014-04-03 20:38:55154bool Hardware::IsOOBEComplete(base::Time* out_time_of_oobe) const {
155 struct stat statbuf;
156 if (stat(kOOBECompletedMarker, &statbuf) != 0) {
157 if (errno != ENOENT) {
158 PLOG(ERROR) << "Error getting information about "
159 << kOOBECompletedMarker;
160 }
161 return false;
162 }
163
164 if (out_time_of_oobe != nullptr)
165 *out_time_of_oobe = base::Time::FromTimeT(statbuf.st_mtime);
166 return true;
167}
168
J. Richard Barnette522d36f2013-10-29 00:22:12169static string ReadValueFromCrosSystem(const string& key) {
J. Richard Barnettec7dd8532013-10-29 23:30:46170 char value_buffer[VB_MAX_STRING_PROPERTY];
J. Richard Barnette522d36f2013-10-29 00:22:12171
J. Richard Barnettec7dd8532013-10-29 23:30:46172 const char *rv = VbGetSystemPropertyString(key.c_str(), value_buffer,
173 sizeof(value_buffer));
174 if (rv != NULL) {
175 string return_value(value_buffer);
J. Richard Barnette522d36f2013-10-29 00:22:12176 TrimWhitespaceASCII(return_value, TRIM_ALL, &return_value);
177 return return_value;
178 }
J. Richard Barnettec7dd8532013-10-29 23:30:46179
180 LOG(ERROR) << "Unable to read crossystem key " << key;
J. Richard Barnette522d36f2013-10-29 00:22:12181 return "";
182}
183
Alex Vakulenkod0fdfb32014-02-21 23:26:26184string Hardware::GetHardwareClass() const {
Chris Masonef8d037f2014-02-19 01:53:00185 if (USE_HWID_OVERRIDE) {
186 return HwidOverride::Read(base::FilePath("/"));
187 }
J. Richard Barnette522d36f2013-10-29 00:22:12188 return ReadValueFromCrosSystem("hwid");
189}
190
Alex Vakulenkod0fdfb32014-02-21 23:26:26191string Hardware::GetFirmwareVersion() const {
J. Richard Barnette522d36f2013-10-29 00:22:12192 return ReadValueFromCrosSystem("fwid");
193}
194
Alex Vakulenkod0fdfb32014-02-21 23:26:26195string Hardware::GetECVersion() const {
J. Richard Barnette522d36f2013-10-29 00:22:12196 string input_line;
197 int exit_code = 0;
Alex Vakulenkod0fdfb32014-02-21 23:26:26198 vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"};
J. Richard Barnette522d36f2013-10-29 00:22:12199
200 bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line);
201 if (!success || exit_code) {
202 LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")";
203 return "";
204 }
205
206 return utils::ParseECVersion(input_line);
207}
208
Alex Deymo42432912013-07-13 03:21:15209} // namespace chromeos_update_engine