blob: 77e00f184adb214197e03bbfa5093069dacbb6a0 [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
Alex Deymo5708ecd2014-04-30 02:44:5063bool Hardware::IsBootDeviceRemovable() const {
64 return utils::IsRemovableDevice(utils::GetDiskName(BootDevice()));
65}
66
Don Garrett83692e42013-11-08 18:11:3067bool Hardware::IsKernelBootable(const std::string& kernel_device,
Alex Vakulenkod0fdfb32014-02-21 23:26:2668 bool* bootable) const {
Don Garrett83692e42013-11-08 18:11:3069 CgptAddParams params;
70 memset(&params, '\0', sizeof(params));
71
Alex Vakulenko4f5b1442014-02-21 20:19:4472 std::string disk_name;
73 int partition_num = 0;
Don Garrett83692e42013-11-08 18:11:3074
Alex Vakulenko4f5b1442014-02-21 20:19:4475 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 18:11:3080
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 23:26:2689std::vector<std::string> Hardware::GetKernelDevices() const {
Alex Vakulenko59e253e2014-02-24 18:40:2190 LOG(INFO) << "GetAllKernelDevices";
91
92 std::string disk_name = utils::GetDiskName(Hardware::BootKernelDevice());
Alex Deymodf632d12014-04-30 03:04:3693 if (disk_name.empty()) {
Alex Vakulenko59e253e2014-02-24 18:40:2194 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-30 03:04:3699 for (int partition_num : {2, 4}) { // for now, only #2, #4 for slot A & B
Alex Vakulenko59e253e2014-02-24 18:40:21100 std::string device = utils::MakePartitionName(disk_name, partition_num);
Alex Deymodf632d12014-04-30 03:04:36101 if (!device.empty()) {
Alex Vakulenko59e253e2014-02-24 18:40:21102 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 18:11:30113bool Hardware::MarkKernelUnbootable(const std::string& kernel_device) {
114 LOG(INFO) << "MarkPartitionUnbootable: " << kernel_device;
115
Don Garrett6646b442013-11-13 23:29:11116 if (kernel_device == BootKernelDevice()) {
117 LOG(ERROR) << "Refusing to mark current kernel as unbootable.";
118 return false;
119 }
120
Alex Vakulenko4f5b1442014-02-21 20:19:44121 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 18:11:30126
127 CgptAddParams params;
128 memset(&params, 0, sizeof(params));
129
Alex Vakulenko4f5b1442014-02-21 20:19:44130 params.drive_name = const_cast<char *>(disk_name.c_str());
131 params.partition = partition_num;
Don Garrett83692e42013-11-08 18:11:30132
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 23:26:26148bool Hardware::IsOfficialBuild() const {
J. Richard Barnettec7dd8532013-10-29 23:30:46149 return VbGetSystemPropertyInt("debug_build") == 0;
J. Richard Barnette056b0ab2013-10-29 22:24:56150}
151
Alex Vakulenkod0fdfb32014-02-21 23:26:26152bool Hardware::IsNormalBootMode() const {
J. Richard Barnettec7dd8532013-10-29 23:30:46153 bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
J. Richard Barnette056b0ab2013-10-29 22:24:56154 LOG_IF(INFO, dev_mode) << "Booted in dev mode.";
155 return !dev_mode;
156}
157
Alex Deymobccbc382014-04-03 20:38:55158bool 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-29 00:22:12173static string ReadValueFromCrosSystem(const string& key) {
J. Richard Barnettec7dd8532013-10-29 23:30:46174 char value_buffer[VB_MAX_STRING_PROPERTY];
J. Richard Barnette522d36f2013-10-29 00:22:12175
J. Richard Barnettec7dd8532013-10-29 23:30:46176 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-22 01:28:22180 base::TrimWhitespaceASCII(return_value, base::TRIM_ALL, &return_value);
J. Richard Barnette522d36f2013-10-29 00:22:12181 return return_value;
182 }
J. Richard Barnettec7dd8532013-10-29 23:30:46183
184 LOG(ERROR) << "Unable to read crossystem key " << key;
J. Richard Barnette522d36f2013-10-29 00:22:12185 return "";
186}
187
Alex Vakulenkod0fdfb32014-02-21 23:26:26188string Hardware::GetHardwareClass() const {
Chris Masonef8d037f2014-02-19 01:53:00189 if (USE_HWID_OVERRIDE) {
190 return HwidOverride::Read(base::FilePath("/"));
191 }
J. Richard Barnette522d36f2013-10-29 00:22:12192 return ReadValueFromCrosSystem("hwid");
193}
194
Alex Vakulenkod0fdfb32014-02-21 23:26:26195string Hardware::GetFirmwareVersion() const {
J. Richard Barnette522d36f2013-10-29 00:22:12196 return ReadValueFromCrosSystem("fwid");
197}
198
Alex Vakulenkod0fdfb32014-02-21 23:26:26199string Hardware::GetECVersion() const {
J. Richard Barnette522d36f2013-10-29 00:22:12200 string input_line;
201 int exit_code = 0;
Alex Vakulenkod0fdfb32014-02-21 23:26:26202 vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"};
J. Richard Barnette522d36f2013-10-29 00:22:12203
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-13 03:21:15213} // namespace chromeos_update_engine