blob: c0f2b678c60ecee7cc8c465cf9182f110b128ce2 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2013 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Alex Deymo42432912013-07-12 20:21:15 -070016
Alex Deymo1b03f9f2015-12-09 00:38:36 -080017#include "update_engine/hardware_chromeos.h"
Alex Deymo42432912013-07-12 20:21:15 -070018
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -080019#include <utility>
20
Alex Deymo46a9aae2016-05-04 20:20:11 -070021#include <base/files/file_path.h>
Ben Chan06c76a42014-09-05 08:21:06 -070022#include <base/files/file_util.h>
Alex Deymo42432912013-07-12 20:21:15 -070023#include <base/logging.h>
Alex Deymoebbe7ef2014-10-30 13:02:49 -070024#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070025#include <base/strings/string_util.h>
Alex Deymo46a9aae2016-05-04 20:20:11 -070026#include <brillo/key_value_store.h>
Sen Jiange67bb5b2016-06-20 15:53:56 -070027#include <debugd/dbus-constants.h>
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070028#include <vboot/crossystem.h>
Alex Deymo42432912013-07-12 20:21:15 -070029
Don Garrett83692e42013-11-08 10:11:30 -080030extern "C" {
31#include "vboot/vboot_host.h"
32}
33
Alex Deymo46a9aae2016-05-04 20:20:11 -070034#include "update_engine/common/constants.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080035#include "update_engine/common/hardware.h"
36#include "update_engine/common/hwid_override.h"
Sen Jiang9c123462015-11-19 13:16:23 -080037#include "update_engine/common/platform_constants.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080038#include "update_engine/common/subprocess.h"
39#include "update_engine/common/utils.h"
Sen Jiange67bb5b2016-06-20 15:53:56 -070040#include "update_engine/dbus_connection.h"
J. Richard Barnette522d36f2013-10-28 17:22:12 -070041
Alex Deymo42432912013-07-12 20:21:15 -070042using std::string;
J. Richard Barnette522d36f2013-10-28 17:22:12 -070043using std::vector;
Alex Deymo42432912013-07-12 20:21:15 -070044
Alex Deymobccbc382014-04-03 13:38:55 -070045namespace {
46
Alex Deymodd132f32015-09-14 19:12:07 -070047const char kOOBECompletedMarker[] = "/home/chronos/.oobe_completed";
48
49// The stateful directory used by update_engine to store powerwash-safe files.
50// The files stored here must be whitelisted in the powerwash scripts.
51const char kPowerwashSafeDirectory[] =
52 "/mnt/stateful_partition/unencrypted/preserve";
Alex Deymobccbc382014-04-03 13:38:55 -070053
Alex Deymoebbe7ef2014-10-30 13:02:49 -070054// The powerwash_count marker file contains the number of times the device was
55// powerwashed. This value is incremented by the clobber-state script when
56// a powerwash is performed.
Alex Deymodd132f32015-09-14 19:12:07 -070057const char kPowerwashCountMarker[] = "powerwash_count";
58
Alex Deymofb905d92016-06-03 19:26:58 -070059// The name of the marker file used to trigger powerwash when post-install
60// completes successfully so that the device is powerwashed on next reboot.
61const char kPowerwashMarkerFile[] =
62 "/mnt/stateful_partition/factory_install_reset";
63
64// The contents of the powerwash marker file.
65const char kPowerwashCommand[] = "safe fast keepimg reason=update_engine\n";
66
Alex Deymo46a9aae2016-05-04 20:20:11 -070067// UpdateManager config path.
68const char* kConfigFilePath = "/etc/update_manager.conf";
69
70// UpdateManager config options:
71const char* kConfigOptsIsOOBEEnabled = "is_oobe_enabled";
72
Amin Hassani1677e812017-06-21 13:36:36 -070073const char* kActivePingKey = "first_active_omaha_ping_sent";
74
Alex Deymobccbc382014-04-03 13:38:55 -070075} // namespace
76
Alex Deymo42432912013-07-12 20:21:15 -070077namespace chromeos_update_engine {
78
Alex Deymo40d86b22015-09-03 22:27:10 -070079namespace hardware {
80
81// Factory defined in hardware.h.
82std::unique_ptr<HardwareInterface> CreateHardware() {
Alex Deymo46a9aae2016-05-04 20:20:11 -070083 std::unique_ptr<HardwareChromeOS> hardware(new HardwareChromeOS());
84 hardware->Init();
85 return std::move(hardware);
Alex Deymo40d86b22015-09-03 22:27:10 -070086}
87
88} // namespace hardware
89
Alex Deymo46a9aae2016-05-04 20:20:11 -070090void HardwareChromeOS::Init() {
91 LoadConfig("" /* root_prefix */, IsNormalBootMode());
Sen Jiange67bb5b2016-06-20 15:53:56 -070092 debugd_proxy_.reset(
93 new org::chromium::debugdProxy(DBusConnection::Get()->GetDBus()));
Alex Deymo46a9aae2016-05-04 20:20:11 -070094}
95
Alex Deymo40d86b22015-09-03 22:27:10 -070096bool HardwareChromeOS::IsOfficialBuild() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070097 return VbGetSystemPropertyInt("debug_build") == 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -070098}
99
Alex Deymo40d86b22015-09-03 22:27:10 -0700100bool HardwareChromeOS::IsNormalBootMode() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700101 bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700102 return !dev_mode;
103}
104
Sen Jiange67bb5b2016-06-20 15:53:56 -0700105bool HardwareChromeOS::AreDevFeaturesEnabled() const {
106 // Even though the debugd tools are also gated on devmode, checking here can
107 // save us a D-Bus call so it's worth doing explicitly.
108 if (IsNormalBootMode())
109 return false;
110
111 int32_t dev_features = debugd::DEV_FEATURES_DISABLED;
112 brillo::ErrorPtr error;
113 // Some boards may not include debugd so it's expected that this may fail,
114 // in which case we treat it as disabled.
115 if (debugd_proxy_ && debugd_proxy_->QueryDevFeatures(&dev_features, &error) &&
116 !(dev_features & debugd::DEV_FEATURES_DISABLED)) {
117 LOG(INFO) << "Debugd dev tools enabled.";
118 return true;
119 }
120 return false;
121}
122
Alex Deymo46a9aae2016-05-04 20:20:11 -0700123bool HardwareChromeOS::IsOOBEEnabled() const {
124 return is_oobe_enabled_;
125}
126
Alex Deymo40d86b22015-09-03 22:27:10 -0700127bool HardwareChromeOS::IsOOBEComplete(base::Time* out_time_of_oobe) const {
Alex Deymo46a9aae2016-05-04 20:20:11 -0700128 if (!is_oobe_enabled_) {
129 LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() was called";
130 }
Alex Deymobccbc382014-04-03 13:38:55 -0700131 struct stat statbuf;
132 if (stat(kOOBECompletedMarker, &statbuf) != 0) {
133 if (errno != ENOENT) {
134 PLOG(ERROR) << "Error getting information about "
135 << kOOBECompletedMarker;
136 }
137 return false;
138 }
139
140 if (out_time_of_oobe != nullptr)
141 *out_time_of_oobe = base::Time::FromTimeT(statbuf.st_mtime);
142 return true;
143}
144
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700145static string ReadValueFromCrosSystem(const string& key) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700146 char value_buffer[VB_MAX_STRING_PROPERTY];
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700147
Alex Deymo40d86b22015-09-03 22:27:10 -0700148 const char* rv = VbGetSystemPropertyString(key.c_str(), value_buffer,
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700149 sizeof(value_buffer));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700150 if (rv != nullptr) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700151 string return_value(value_buffer);
Ben Chan736fcb52014-05-21 18:28:22 -0700152 base::TrimWhitespaceASCII(return_value, base::TRIM_ALL, &return_value);
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700153 return return_value;
154 }
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700155
156 LOG(ERROR) << "Unable to read crossystem key " << key;
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700157 return "";
158}
159
Alex Deymo40d86b22015-09-03 22:27:10 -0700160string HardwareChromeOS::GetHardwareClass() const {
Chris Masonef8d037f2014-02-19 01:53:00 +0000161 if (USE_HWID_OVERRIDE) {
162 return HwidOverride::Read(base::FilePath("/"));
163 }
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700164 return ReadValueFromCrosSystem("hwid");
165}
166
Alex Deymo40d86b22015-09-03 22:27:10 -0700167string HardwareChromeOS::GetFirmwareVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700168 return ReadValueFromCrosSystem("fwid");
169}
170
Alex Deymo40d86b22015-09-03 22:27:10 -0700171string HardwareChromeOS::GetECVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700172 string input_line;
173 int exit_code = 0;
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800174 vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"};
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700175
176 bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line);
177 if (!success || exit_code) {
178 LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")";
179 return "";
180 }
181
182 return utils::ParseECVersion(input_line);
183}
184
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800185int HardwareChromeOS::GetMinKernelKeyVersion() const {
186 return VbGetSystemPropertyInt("tpm_kernver");
187}
188
Marton Hunyady99ced782018-05-08 12:59:50 +0200189int HardwareChromeOS::GetMinFirmwareKeyVersion() const {
190 return VbGetSystemPropertyInt("tpm_fwver");
191}
192
Zentaro Kavanagh5d956152018-05-15 09:40:33 -0700193bool HardwareChromeOS::SetMaxKernelKeyRollforward(int kernel_max_rollforward) {
194 return VbSetSystemPropertyInt("kernel_max_rollforward",
195 kernel_max_rollforward) == 0;
Zentaro Kavanaghbaacb982018-02-20 17:48:39 -0800196}
197
Alex Deymo40d86b22015-09-03 22:27:10 -0700198int HardwareChromeOS::GetPowerwashCount() const {
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700199 int powerwash_count;
Alex Deymodd132f32015-09-14 19:12:07 -0700200 base::FilePath marker_path = base::FilePath(kPowerwashSafeDirectory).Append(
201 kPowerwashCountMarker);
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700202 string contents;
Alex Deymodd132f32015-09-14 19:12:07 -0700203 if (!utils::ReadFile(marker_path.value(), &contents))
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700204 return -1;
205 base::TrimWhitespaceASCII(contents, base::TRIM_TRAILING, &contents);
206 if (!base::StringToInt(contents, &powerwash_count))
207 return -1;
208 return powerwash_count;
209}
210
Alex Deymofb905d92016-06-03 19:26:58 -0700211bool HardwareChromeOS::SchedulePowerwash() {
212 bool result = utils::WriteFile(
213 kPowerwashMarkerFile, kPowerwashCommand, strlen(kPowerwashCommand));
214 if (result) {
Alex Deymocaa46722016-06-09 12:08:29 -0700215 LOG(INFO) << "Created " << kPowerwashMarkerFile
216 << " to powerwash on next reboot";
Alex Deymofb905d92016-06-03 19:26:58 -0700217 } else {
Alex Deymocaa46722016-06-09 12:08:29 -0700218 PLOG(ERROR) << "Error in creating powerwash marker file: "
219 << kPowerwashMarkerFile;
Alex Deymofb905d92016-06-03 19:26:58 -0700220 }
221
222 return result;
223}
224
225bool HardwareChromeOS::CancelPowerwash() {
226 bool result = base::DeleteFile(base::FilePath(kPowerwashMarkerFile), false);
227
228 if (result) {
229 LOG(INFO) << "Successfully deleted the powerwash marker file : "
Alex Deymocaa46722016-06-09 12:08:29 -0700230 << kPowerwashMarkerFile;
Alex Deymofb905d92016-06-03 19:26:58 -0700231 } else {
232 PLOG(ERROR) << "Could not delete the powerwash marker file : "
Alex Deymocaa46722016-06-09 12:08:29 -0700233 << kPowerwashMarkerFile;
Alex Deymofb905d92016-06-03 19:26:58 -0700234 }
235
236 return result;
237}
238
Alex Deymodd132f32015-09-14 19:12:07 -0700239bool HardwareChromeOS::GetNonVolatileDirectory(base::FilePath* path) const {
Sen Jiang9c123462015-11-19 13:16:23 -0800240 *path = base::FilePath(constants::kNonVolatileDirectory);
Alex Deymodd132f32015-09-14 19:12:07 -0700241 return true;
242}
243
244bool HardwareChromeOS::GetPowerwashSafeDirectory(base::FilePath* path) const {
245 *path = base::FilePath(kPowerwashSafeDirectory);
246 return true;
247}
248
Alex Deymo46a9aae2016-05-04 20:20:11 -0700249void HardwareChromeOS::LoadConfig(const string& root_prefix, bool normal_mode) {
250 brillo::KeyValueStore store;
251
252 if (normal_mode) {
253 store.Load(base::FilePath(root_prefix + kConfigFilePath));
254 } else {
255 if (store.Load(base::FilePath(root_prefix + kStatefulPartition +
256 kConfigFilePath))) {
257 LOG(INFO) << "UpdateManager Config loaded from stateful partition.";
258 } else {
259 store.Load(base::FilePath(root_prefix + kConfigFilePath));
260 }
261 }
262
263 if (!store.GetBoolean(kConfigOptsIsOOBEEnabled, &is_oobe_enabled_))
264 is_oobe_enabled_ = true; // Default value.
265}
266
Amin Hassani1677e812017-06-21 13:36:36 -0700267bool HardwareChromeOS::GetFirstActiveOmahaPingSent() const {
268 int exit_code = 0;
269 string active_ping_str;
270 vector<string> cmd = { "vpd_get_value", kActivePingKey };
271 if (!Subprocess::SynchronousExec(cmd, &exit_code, &active_ping_str) ||
272 exit_code) {
273 LOG(ERROR) << "Failed to get vpd key for " << kActivePingKey
274 << " with exit code: " << exit_code;
275 return false;
276 }
277
278 base::TrimWhitespaceASCII(active_ping_str,
279 base::TRIM_ALL,
280 &active_ping_str);
281 int active_ping;
282 if (active_ping_str.empty() ||
283 !base::StringToInt(active_ping_str, &active_ping)) {
284 LOG(INFO) << "Failed to parse active_ping value: " << active_ping_str;
285 return false;
286 }
287 return static_cast<bool>(active_ping);
288}
289
290void HardwareChromeOS::SetFirstActiveOmahaPingSent() {
291 int exit_code = 0;
292 string output;
293 vector<string> vpd_set_cmd = {
294 "vpd", "-i", "RW_VPD", "-s", string(kActivePingKey) + "=1" };
295 if (!Subprocess::SynchronousExec(vpd_set_cmd, &exit_code, &output) ||
296 exit_code) {
297 LOG(ERROR) << "Failed to set vpd key for " << kActivePingKey
298 << " with exit code: " << exit_code
299 << " with error: " << output;
300 return;
301 }
302
303 vector<string> vpd_dump_cmd = { "dump_vpd_log", "--force" };
304 if (!Subprocess::SynchronousExec(vpd_dump_cmd, &exit_code, &output) ||
305 exit_code) {
306 LOG(ERROR) << "Failed to cache " << kActivePingKey<< " using dump_vpd_log"
307 << " with exit code: " << exit_code
308 << " with error: " << output;
309 }
310}
311
Alex Deymo42432912013-07-12 20:21:15 -0700312} // namespace chromeos_update_engine