blob: 5d5492f6c489fe4c5646f6a64ef0c2ecb879e049 [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>
Alex Vakulenko75039d72014-03-25 12:36:28 -07009#include <base/strings/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
Alex Deymobccbc382014-04-03 13:38:55 -070030namespace {
31
32static const char kOOBECompletedMarker[] = "/home/chronos/.oobe_completed";
33
34} // namespace
35
Alex Deymo42432912013-07-12 20:21:15 -070036namespace chromeos_update_engine {
37
Chris Masonef8d037f2014-02-19 01:53:00 +000038Hardware::Hardware() {}
39
40Hardware::~Hardware() {}
41
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080042string Hardware::BootKernelDevice() const {
Don Garrett83692e42013-11-08 10:11:30 -080043 return utils::KernelDeviceOfBootDevice(Hardware::BootDevice());
44}
45
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080046string Hardware::BootDevice() const {
Alex Deymo42432912013-07-12 20:21:15 -070047 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 10:11:30 -080063bool Hardware::IsKernelBootable(const std::string& kernel_device,
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080064 bool* bootable) const {
Don Garrett83692e42013-11-08 10:11:30 -080065 CgptAddParams params;
66 memset(&params, '\0', sizeof(params));
67
Alex Vakulenko4f5b1442014-02-21 12:19:44 -080068 std::string disk_name;
69 int partition_num = 0;
Don Garrett83692e42013-11-08 10:11:30 -080070
Alex Vakulenko4f5b1442014-02-21 12:19:44 -080071 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 10:11:30 -080076
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 15:26:26 -080085std::vector<std::string> Hardware::GetKernelDevices() const {
Alex Vakulenko59e253e2014-02-24 10:40:21 -080086 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 13:23:46 -070095 for (int partition_num : {2, 4}) { // for now, only #2, #4 for slot A & B
Alex Vakulenko59e253e2014-02-24 10:40:21 -080096 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 10:11:30 -0800109bool Hardware::MarkKernelUnbootable(const std::string& kernel_device) {
110 LOG(INFO) << "MarkPartitionUnbootable: " << kernel_device;
111
Don Garrett6646b442013-11-13 15:29:11 -0800112 if (kernel_device == BootKernelDevice()) {
113 LOG(ERROR) << "Refusing to mark current kernel as unbootable.";
114 return false;
115 }
116
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800117 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 10:11:30 -0800122
123 CgptAddParams params;
124 memset(&params, 0, sizeof(params));
125
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800126 params.drive_name = const_cast<char *>(disk_name.c_str());
127 params.partition = partition_num;
Don Garrett83692e42013-11-08 10:11:30 -0800128
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 15:26:26 -0800144bool Hardware::IsOfficialBuild() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700145 return VbGetSystemPropertyInt("debug_build") == 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700146}
147
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800148bool Hardware::IsNormalBootMode() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700149 bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700150 LOG_IF(INFO, dev_mode) << "Booted in dev mode.";
151 return !dev_mode;
152}
153
Alex Deymobccbc382014-04-03 13:38:55 -0700154bool 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-28 17:22:12 -0700169static string ReadValueFromCrosSystem(const string& key) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700170 char value_buffer[VB_MAX_STRING_PROPERTY];
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700171
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700172 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-28 17:22:12 -0700176 TrimWhitespaceASCII(return_value, TRIM_ALL, &return_value);
177 return return_value;
178 }
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700179
180 LOG(ERROR) << "Unable to read crossystem key " << key;
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700181 return "";
182}
183
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800184string Hardware::GetHardwareClass() const {
Chris Masonef8d037f2014-02-19 01:53:00 +0000185 if (USE_HWID_OVERRIDE) {
186 return HwidOverride::Read(base::FilePath("/"));
187 }
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700188 return ReadValueFromCrosSystem("hwid");
189}
190
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800191string Hardware::GetFirmwareVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700192 return ReadValueFromCrosSystem("fwid");
193}
194
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800195string Hardware::GetECVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700196 string input_line;
197 int exit_code = 0;
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800198 vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"};
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700199
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-12 20:21:15 -0700209} // namespace chromeos_update_engine