blob: 77e00f184adb214197e03bbfa5093069dacbb6a0 [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
Alex Deymo5708ecd2014-04-29 19:44:50 -070063bool Hardware::IsBootDeviceRemovable() const {
64 return utils::IsRemovableDevice(utils::GetDiskName(BootDevice()));
65}
66
Don Garrett83692e42013-11-08 10:11:30 -080067bool Hardware::IsKernelBootable(const std::string& kernel_device,
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080068 bool* bootable) const {
Don Garrett83692e42013-11-08 10:11:30 -080069 CgptAddParams params;
70 memset(&params, '\0', sizeof(params));
71
Alex Vakulenko4f5b1442014-02-21 12:19:44 -080072 std::string disk_name;
73 int partition_num = 0;
Don Garrett83692e42013-11-08 10:11:30 -080074
Alex Vakulenko4f5b1442014-02-21 12:19:44 -080075 if (!utils::SplitPartitionName(kernel_device, &disk_name, &partition_num))
76 return false;
77
78 params.drive_name = const_cast<char *>(disk_name.c_str());
79 params.partition = partition_num;
Don Garrett83692e42013-11-08 10:11:30 -080080
81 int retval = CgptGetPartitionDetails(&params);
82 if (retval != CGPT_OK)
83 return false;
84
85 *bootable = params.successful || (params.tries > 0);
86 return true;
87}
88
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -080089std::vector<std::string> Hardware::GetKernelDevices() const {
Alex Vakulenko59e253e2014-02-24 10:40:21 -080090 LOG(INFO) << "GetAllKernelDevices";
91
92 std::string disk_name = utils::GetDiskName(Hardware::BootKernelDevice());
Alex Deymodf632d12014-04-29 20:04:36 -070093 if (disk_name.empty()) {
Alex Vakulenko59e253e2014-02-24 10:40:21 -080094 LOG(ERROR) << "Failed to get the cuurent kernel boot disk name";
95 return std::vector<std::string>();
96 }
97
98 std::vector<std::string> devices;
Alex Deymodf632d12014-04-29 20:04:36 -070099 for (int partition_num : {2, 4}) { // for now, only #2, #4 for slot A & B
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800100 std::string device = utils::MakePartitionName(disk_name, partition_num);
Alex Deymodf632d12014-04-29 20:04:36 -0700101 if (!device.empty()) {
Alex Vakulenko59e253e2014-02-24 10:40:21 -0800102 devices.push_back(std::move(device));
103 } else {
104 LOG(ERROR) << "Cannot make a partition name for disk: "
105 << disk_name << ", partition: " << partition_num;
106 }
107 }
108
109 return devices;
110}
111
112
Don Garrett83692e42013-11-08 10:11:30 -0800113bool Hardware::MarkKernelUnbootable(const std::string& kernel_device) {
114 LOG(INFO) << "MarkPartitionUnbootable: " << kernel_device;
115
Don Garrett6646b442013-11-13 15:29:11 -0800116 if (kernel_device == BootKernelDevice()) {
117 LOG(ERROR) << "Refusing to mark current kernel as unbootable.";
118 return false;
119 }
120
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800121 std::string disk_name;
122 int partition_num = 0;
123
124 if (!utils::SplitPartitionName(kernel_device, &disk_name, &partition_num))
125 return false;
Don Garrett83692e42013-11-08 10:11:30 -0800126
127 CgptAddParams params;
128 memset(&params, 0, sizeof(params));
129
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800130 params.drive_name = const_cast<char *>(disk_name.c_str());
131 params.partition = partition_num;
Don Garrett83692e42013-11-08 10:11:30 -0800132
133 params.successful = false;
134 params.set_successful = true;
135
136 params.tries = 0;
137 params.set_tries = true;
138
139 int retval = CgptSetAttributes(&params);
140 if (retval != CGPT_OK) {
141 LOG(ERROR) << "Marking kernel unbootable failed.";
142 return false;
143 }
144
145 return true;
146}
147
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800148bool Hardware::IsOfficialBuild() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700149 return VbGetSystemPropertyInt("debug_build") == 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700150}
151
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800152bool Hardware::IsNormalBootMode() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700153 bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700154 LOG_IF(INFO, dev_mode) << "Booted in dev mode.";
155 return !dev_mode;
156}
157
Alex Deymobccbc382014-04-03 13:38:55 -0700158bool Hardware::IsOOBEComplete(base::Time* out_time_of_oobe) const {
159 struct stat statbuf;
160 if (stat(kOOBECompletedMarker, &statbuf) != 0) {
161 if (errno != ENOENT) {
162 PLOG(ERROR) << "Error getting information about "
163 << kOOBECompletedMarker;
164 }
165 return false;
166 }
167
168 if (out_time_of_oobe != nullptr)
169 *out_time_of_oobe = base::Time::FromTimeT(statbuf.st_mtime);
170 return true;
171}
172
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700173static string ReadValueFromCrosSystem(const string& key) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700174 char value_buffer[VB_MAX_STRING_PROPERTY];
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700175
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700176 const char *rv = VbGetSystemPropertyString(key.c_str(), value_buffer,
177 sizeof(value_buffer));
178 if (rv != NULL) {
179 string return_value(value_buffer);
Ben Chan736fcb52014-05-21 18:28:22 -0700180 base::TrimWhitespaceASCII(return_value, base::TRIM_ALL, &return_value);
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700181 return return_value;
182 }
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700183
184 LOG(ERROR) << "Unable to read crossystem key " << key;
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700185 return "";
186}
187
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800188string Hardware::GetHardwareClass() const {
Chris Masonef8d037f2014-02-19 01:53:00 +0000189 if (USE_HWID_OVERRIDE) {
190 return HwidOverride::Read(base::FilePath("/"));
191 }
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700192 return ReadValueFromCrosSystem("hwid");
193}
194
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800195string Hardware::GetFirmwareVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700196 return ReadValueFromCrosSystem("fwid");
197}
198
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800199string Hardware::GetECVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700200 string input_line;
201 int exit_code = 0;
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800202 vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"};
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700203
204 bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line);
205 if (!success || exit_code) {
206 LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")";
207 return "";
208 }
209
210 return utils::ParseECVersion(input_line);
211}
212
Alex Deymo42432912013-07-12 20:21:15 -0700213} // namespace chromeos_update_engine