blob: fd774ca06cdb2071b9fe62ff649128341ae8dd59 [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
Ben Chan06c76a42014-09-05 08:21:06 -07007#include <base/files/file_util.h>
Alex Deymo42432912013-07-12 20:21:15 -07008#include <base/logging.h>
Alex Deymoebbe7ef2014-10-30 13:02:49 -07009#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070010#include <base/strings/string_util.h>
Alex Deymo42432912013-07-12 20:21:15 -070011#include <rootdev/rootdev.h>
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070012#include <vboot/crossystem.h>
Alex Deymo42432912013-07-12 20:21:15 -070013
Don Garrett83692e42013-11-08 10:11:30 -080014extern "C" {
15#include "vboot/vboot_host.h"
16}
17
Chris Masonef8d037f2014-02-19 01:53:00 +000018#include "update_engine/hwid_override.h"
J. Richard Barnette522d36f2013-10-28 17:22:12 -070019#include "update_engine/subprocess.h"
20#include "update_engine/utils.h"
21
Alex Deymo42432912013-07-12 20:21:15 -070022using std::string;
J. Richard Barnette522d36f2013-10-28 17:22:12 -070023using std::vector;
Alex Deymo42432912013-07-12 20:21:15 -070024
Alex Deymobccbc382014-04-03 13:38:55 -070025namespace {
26
27static const char kOOBECompletedMarker[] = "/home/chronos/.oobe_completed";
28
Alex Deymoebbe7ef2014-10-30 13:02:49 -070029// The powerwash_count marker file contains the number of times the device was
30// powerwashed. This value is incremented by the clobber-state script when
31// a powerwash is performed.
32static const char kPowerwashCountMarker[] =
33 "/mnt/stateful_partition/unencrypted/preserve/powerwash_count";
34
Alex Deymobccbc382014-04-03 13:38:55 -070035} // namespace
36
Alex Deymo42432912013-07-12 20:21:15 -070037namespace chromeos_update_engine {
38
Chris Masonef8d037f2014-02-19 01:53:00 +000039Hardware::Hardware() {}
40
41Hardware::~Hardware() {}
42
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080043string Hardware::BootKernelDevice() const {
Don Garrett83692e42013-11-08 10:11:30 -080044 return utils::KernelDeviceOfBootDevice(Hardware::BootDevice());
45}
46
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080047string Hardware::BootDevice() const {
Alex Deymo42432912013-07-12 20:21:15 -070048 char boot_path[PATH_MAX];
49 // Resolve the boot device path fully, including dereferencing
50 // through dm-verity.
51 int ret = rootdev(boot_path, sizeof(boot_path), true, false);
52
53 if (ret < 0) {
54 LOG(ERROR) << "rootdev failed to find the root device";
55 return "";
56 }
57 LOG_IF(WARNING, ret > 0) << "rootdev found a device name with no device node";
58
59 // This local variable is used to construct the return string and is not
60 // passed around after use.
61 return boot_path;
62}
63
Alex Deymo5708ecd2014-04-29 19:44:50 -070064bool Hardware::IsBootDeviceRemovable() const {
65 return utils::IsRemovableDevice(utils::GetDiskName(BootDevice()));
66}
67
Don Garrett83692e42013-11-08 10:11:30 -080068bool Hardware::IsKernelBootable(const std::string& kernel_device,
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080069 bool* bootable) const {
Don Garrett83692e42013-11-08 10:11:30 -080070 CgptAddParams params;
71 memset(&params, '\0', sizeof(params));
72
Alex Vakulenko4f5b1442014-02-21 12:19:44 -080073 std::string disk_name;
74 int partition_num = 0;
Don Garrett83692e42013-11-08 10:11:30 -080075
Alex Vakulenko4f5b1442014-02-21 12:19:44 -080076 if (!utils::SplitPartitionName(kernel_device, &disk_name, &partition_num))
77 return false;
78
79 params.drive_name = const_cast<char *>(disk_name.c_str());
80 params.partition = partition_num;
Don Garrett83692e42013-11-08 10:11:30 -080081
82 int retval = CgptGetPartitionDetails(&params);
83 if (retval != CGPT_OK)
84 return false;
85
86 *bootable = params.successful || (params.tries > 0);
87 return true;
88}
89
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080090std::vector<std::string> Hardware::GetKernelDevices() const {
Alex Vakulenko59e253e2014-02-24 10:40:21 -080091 LOG(INFO) << "GetAllKernelDevices";
92
93 std::string disk_name = utils::GetDiskName(Hardware::BootKernelDevice());
Alex Deymodf632d12014-04-29 20:04:36 -070094 if (disk_name.empty()) {
Alex Vakulenko072359c2014-07-18 11:41:07 -070095 LOG(ERROR) << "Failed to get the current kernel boot disk name";
Alex Vakulenko59e253e2014-02-24 10:40:21 -080096 return std::vector<std::string>();
97 }
98
99 std::vector<std::string> devices;
Alex Deymodf632d12014-04-29 20:04:36 -0700100 for (int partition_num : {2, 4}) { // for now, only #2, #4 for slot A & B
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800101 std::string device = utils::MakePartitionName(disk_name, partition_num);
Alex Deymodf632d12014-04-29 20:04:36 -0700102 if (!device.empty()) {
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800103 devices.push_back(std::move(device));
104 } else {
105 LOG(ERROR) << "Cannot make a partition name for disk: "
106 << disk_name << ", partition: " << partition_num;
107 }
108 }
109
110 return devices;
111}
112
113
Don Garrett83692e42013-11-08 10:11:30 -0800114bool Hardware::MarkKernelUnbootable(const std::string& kernel_device) {
115 LOG(INFO) << "MarkPartitionUnbootable: " << kernel_device;
116
Don Garrett6646b442013-11-13 15:29:11 -0800117 if (kernel_device == BootKernelDevice()) {
118 LOG(ERROR) << "Refusing to mark current kernel as unbootable.";
119 return false;
120 }
121
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800122 std::string disk_name;
123 int partition_num = 0;
124
125 if (!utils::SplitPartitionName(kernel_device, &disk_name, &partition_num))
126 return false;
Don Garrett83692e42013-11-08 10:11:30 -0800127
128 CgptAddParams params;
129 memset(&params, 0, sizeof(params));
130
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800131 params.drive_name = const_cast<char *>(disk_name.c_str());
132 params.partition = partition_num;
Don Garrett83692e42013-11-08 10:11:30 -0800133
134 params.successful = false;
135 params.set_successful = true;
136
137 params.tries = 0;
138 params.set_tries = true;
139
140 int retval = CgptSetAttributes(&params);
141 if (retval != CGPT_OK) {
142 LOG(ERROR) << "Marking kernel unbootable failed.";
143 return false;
144 }
145
146 return true;
147}
148
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800149bool Hardware::IsOfficialBuild() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700150 return VbGetSystemPropertyInt("debug_build") == 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700151}
152
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800153bool Hardware::IsNormalBootMode() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700154 bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700155 LOG_IF(INFO, dev_mode) << "Booted in dev mode.";
156 return !dev_mode;
157}
158
Alex Deymobccbc382014-04-03 13:38:55 -0700159bool Hardware::IsOOBEComplete(base::Time* out_time_of_oobe) const {
160 struct stat statbuf;
161 if (stat(kOOBECompletedMarker, &statbuf) != 0) {
162 if (errno != ENOENT) {
163 PLOG(ERROR) << "Error getting information about "
164 << kOOBECompletedMarker;
165 }
166 return false;
167 }
168
169 if (out_time_of_oobe != nullptr)
170 *out_time_of_oobe = base::Time::FromTimeT(statbuf.st_mtime);
171 return true;
172}
173
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700174static string ReadValueFromCrosSystem(const string& key) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700175 char value_buffer[VB_MAX_STRING_PROPERTY];
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700176
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700177 const char *rv = VbGetSystemPropertyString(key.c_str(), value_buffer,
178 sizeof(value_buffer));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700179 if (rv != nullptr) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700180 string return_value(value_buffer);
Ben Chan736fcb52014-05-21 18:28:22 -0700181 base::TrimWhitespaceASCII(return_value, base::TRIM_ALL, &return_value);
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700182 return return_value;
183 }
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700184
185 LOG(ERROR) << "Unable to read crossystem key " << key;
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700186 return "";
187}
188
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800189string Hardware::GetHardwareClass() const {
Chris Masonef8d037f2014-02-19 01:53:00 +0000190 if (USE_HWID_OVERRIDE) {
191 return HwidOverride::Read(base::FilePath("/"));
192 }
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700193 return ReadValueFromCrosSystem("hwid");
194}
195
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800196string Hardware::GetFirmwareVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700197 return ReadValueFromCrosSystem("fwid");
198}
199
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800200string Hardware::GetECVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700201 string input_line;
202 int exit_code = 0;
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800203 vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"};
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700204
205 bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line);
206 if (!success || exit_code) {
207 LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")";
208 return "";
209 }
210
211 return utils::ParseECVersion(input_line);
212}
213
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700214int Hardware::GetPowerwashCount() const {
215 int powerwash_count;
216 string contents;
217 if (!utils::ReadFile(kPowerwashCountMarker, &contents))
218 return -1;
219 base::TrimWhitespaceASCII(contents, base::TRIM_TRAILING, &contents);
220 if (!base::StringToInt(contents, &powerwash_count))
221 return -1;
222 return powerwash_count;
223}
224
Alex Deymo42432912013-07-12 20:21:15 -0700225} // namespace chromeos_update_engine