blob: 74de45a6946ef03595f9120d15e5221f1962932d [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
Chris Masonef8d037f2014-02-19 01:53:00 +000017#include "update_engine/hwid_override.h"
J. Richard Barnette522d36f2013-10-28 17:22:12 -070018#include "update_engine/subprocess.h"
19#include "update_engine/utils.h"
20
Alex Deymo42432912013-07-12 20:21:15 -070021using std::string;
J. Richard Barnette522d36f2013-10-28 17:22:12 -070022using std::vector;
Alex Deymo42432912013-07-12 20:21:15 -070023
Alex Deymobccbc382014-04-03 13:38:55 -070024namespace {
25
26static const char kOOBECompletedMarker[] = "/home/chronos/.oobe_completed";
27
28} // namespace
29
Alex Deymo42432912013-07-12 20:21:15 -070030namespace chromeos_update_engine {
31
Chris Masonef8d037f2014-02-19 01:53:00 +000032Hardware::Hardware() {}
33
34Hardware::~Hardware() {}
35
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080036string Hardware::BootKernelDevice() const {
Don Garrett83692e42013-11-08 10:11:30 -080037 return utils::KernelDeviceOfBootDevice(Hardware::BootDevice());
38}
39
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080040string Hardware::BootDevice() const {
Alex Deymo42432912013-07-12 20:21:15 -070041 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
Alex Deymo5708ecd2014-04-29 19:44:50 -070057bool Hardware::IsBootDeviceRemovable() const {
58 return utils::IsRemovableDevice(utils::GetDiskName(BootDevice()));
59}
60
Don Garrett83692e42013-11-08 10:11:30 -080061bool Hardware::IsKernelBootable(const std::string& kernel_device,
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080062 bool* bootable) const {
Don Garrett83692e42013-11-08 10:11:30 -080063 CgptAddParams params;
64 memset(&params, '\0', sizeof(params));
65
Alex Vakulenko4f5b1442014-02-21 12:19:44 -080066 std::string disk_name;
67 int partition_num = 0;
Don Garrett83692e42013-11-08 10:11:30 -080068
Alex Vakulenko4f5b1442014-02-21 12:19:44 -080069 if (!utils::SplitPartitionName(kernel_device, &disk_name, &partition_num))
70 return false;
71
72 params.drive_name = const_cast<char *>(disk_name.c_str());
73 params.partition = partition_num;
Don Garrett83692e42013-11-08 10:11:30 -080074
75 int retval = CgptGetPartitionDetails(&params);
76 if (retval != CGPT_OK)
77 return false;
78
79 *bootable = params.successful || (params.tries > 0);
80 return true;
81}
82
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080083std::vector<std::string> Hardware::GetKernelDevices() const {
Alex Vakulenko59e253e2014-02-24 10:40:21 -080084 LOG(INFO) << "GetAllKernelDevices";
85
86 std::string disk_name = utils::GetDiskName(Hardware::BootKernelDevice());
Alex Deymodf632d12014-04-29 20:04:36 -070087 if (disk_name.empty()) {
Alex Vakulenko072359c2014-07-18 11:41:07 -070088 LOG(ERROR) << "Failed to get the current kernel boot disk name";
Alex Vakulenko59e253e2014-02-24 10:40:21 -080089 return std::vector<std::string>();
90 }
91
92 std::vector<std::string> devices;
Alex Deymodf632d12014-04-29 20:04:36 -070093 for (int partition_num : {2, 4}) { // for now, only #2, #4 for slot A & B
Alex Vakulenko59e253e2014-02-24 10:40:21 -080094 std::string device = utils::MakePartitionName(disk_name, partition_num);
Alex Deymodf632d12014-04-29 20:04:36 -070095 if (!device.empty()) {
Alex Vakulenko59e253e2014-02-24 10:40:21 -080096 devices.push_back(std::move(device));
97 } else {
98 LOG(ERROR) << "Cannot make a partition name for disk: "
99 << disk_name << ", partition: " << partition_num;
100 }
101 }
102
103 return devices;
104}
105
106
Don Garrett83692e42013-11-08 10:11:30 -0800107bool Hardware::MarkKernelUnbootable(const std::string& kernel_device) {
108 LOG(INFO) << "MarkPartitionUnbootable: " << kernel_device;
109
Don Garrett6646b442013-11-13 15:29:11 -0800110 if (kernel_device == BootKernelDevice()) {
111 LOG(ERROR) << "Refusing to mark current kernel as unbootable.";
112 return false;
113 }
114
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800115 std::string disk_name;
116 int partition_num = 0;
117
118 if (!utils::SplitPartitionName(kernel_device, &disk_name, &partition_num))
119 return false;
Don Garrett83692e42013-11-08 10:11:30 -0800120
121 CgptAddParams params;
122 memset(&params, 0, sizeof(params));
123
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800124 params.drive_name = const_cast<char *>(disk_name.c_str());
125 params.partition = partition_num;
Don Garrett83692e42013-11-08 10:11:30 -0800126
127 params.successful = false;
128 params.set_successful = true;
129
130 params.tries = 0;
131 params.set_tries = true;
132
133 int retval = CgptSetAttributes(&params);
134 if (retval != CGPT_OK) {
135 LOG(ERROR) << "Marking kernel unbootable failed.";
136 return false;
137 }
138
139 return true;
140}
141
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800142bool Hardware::IsOfficialBuild() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700143 return VbGetSystemPropertyInt("debug_build") == 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700144}
145
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800146bool Hardware::IsNormalBootMode() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700147 bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700148 LOG_IF(INFO, dev_mode) << "Booted in dev mode.";
149 return !dev_mode;
150}
151
Alex Deymobccbc382014-04-03 13:38:55 -0700152bool Hardware::IsOOBEComplete(base::Time* out_time_of_oobe) const {
153 struct stat statbuf;
154 if (stat(kOOBECompletedMarker, &statbuf) != 0) {
155 if (errno != ENOENT) {
156 PLOG(ERROR) << "Error getting information about "
157 << kOOBECompletedMarker;
158 }
159 return false;
160 }
161
162 if (out_time_of_oobe != nullptr)
163 *out_time_of_oobe = base::Time::FromTimeT(statbuf.st_mtime);
164 return true;
165}
166
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700167static string ReadValueFromCrosSystem(const string& key) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700168 char value_buffer[VB_MAX_STRING_PROPERTY];
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700169
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700170 const char *rv = VbGetSystemPropertyString(key.c_str(), value_buffer,
171 sizeof(value_buffer));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700172 if (rv != nullptr) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700173 string return_value(value_buffer);
Ben Chan736fcb52014-05-21 18:28:22 -0700174 base::TrimWhitespaceASCII(return_value, base::TRIM_ALL, &return_value);
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700175 return return_value;
176 }
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700177
178 LOG(ERROR) << "Unable to read crossystem key " << key;
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700179 return "";
180}
181
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800182string Hardware::GetHardwareClass() const {
Chris Masonef8d037f2014-02-19 01:53:00 +0000183 if (USE_HWID_OVERRIDE) {
184 return HwidOverride::Read(base::FilePath("/"));
185 }
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700186 return ReadValueFromCrosSystem("hwid");
187}
188
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800189string Hardware::GetFirmwareVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700190 return ReadValueFromCrosSystem("fwid");
191}
192
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800193string Hardware::GetECVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700194 string input_line;
195 int exit_code = 0;
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800196 vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"};
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700197
198 bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line);
199 if (!success || exit_code) {
200 LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")";
201 return "";
202 }
203
204 return utils::ParseECVersion(input_line);
205}
206
Alex Deymo42432912013-07-12 20:21:15 -0700207} // namespace chromeos_update_engine