blob: 8c19aa7f4f37fde4dae92588efa7854949b2f42a [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
Alex Deymo46a9aae2016-05-04 20:20:11 -070019#include <base/files/file_path.h>
Ben Chan06c76a42014-09-05 08:21:06 -070020#include <base/files/file_util.h>
Alex Deymo42432912013-07-12 20:21:15 -070021#include <base/logging.h>
Alex Deymoebbe7ef2014-10-30 13:02:49 -070022#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070023#include <base/strings/string_util.h>
Alex Deymo46a9aae2016-05-04 20:20:11 -070024#include <brillo/key_value_store.h>
Sen Jiange67bb5b2016-06-20 15:53:56 -070025#include <debugd/dbus-constants.h>
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070026#include <vboot/crossystem.h>
Alex Deymo42432912013-07-12 20:21:15 -070027
Don Garrett83692e42013-11-08 10:11:30 -080028extern "C" {
29#include "vboot/vboot_host.h"
30}
31
Alex Deymo46a9aae2016-05-04 20:20:11 -070032#include "update_engine/common/constants.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080033#include "update_engine/common/hardware.h"
34#include "update_engine/common/hwid_override.h"
Sen Jiang9c123462015-11-19 13:16:23 -080035#include "update_engine/common/platform_constants.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080036#include "update_engine/common/subprocess.h"
37#include "update_engine/common/utils.h"
Sen Jiange67bb5b2016-06-20 15:53:56 -070038#include "update_engine/dbus_connection.h"
J. Richard Barnette522d36f2013-10-28 17:22:12 -070039
Alex Deymo42432912013-07-12 20:21:15 -070040using std::string;
J. Richard Barnette522d36f2013-10-28 17:22:12 -070041using std::vector;
Alex Deymo42432912013-07-12 20:21:15 -070042
Alex Deymobccbc382014-04-03 13:38:55 -070043namespace {
44
Alex Deymodd132f32015-09-14 19:12:07 -070045const char kOOBECompletedMarker[] = "/home/chronos/.oobe_completed";
46
47// The stateful directory used by update_engine to store powerwash-safe files.
48// The files stored here must be whitelisted in the powerwash scripts.
49const char kPowerwashSafeDirectory[] =
50 "/mnt/stateful_partition/unencrypted/preserve";
Alex Deymobccbc382014-04-03 13:38:55 -070051
Alex Deymoebbe7ef2014-10-30 13:02:49 -070052// The powerwash_count marker file contains the number of times the device was
53// powerwashed. This value is incremented by the clobber-state script when
54// a powerwash is performed.
Alex Deymodd132f32015-09-14 19:12:07 -070055const char kPowerwashCountMarker[] = "powerwash_count";
56
Alex Deymofb905d92016-06-03 19:26:58 -070057// The name of the marker file used to trigger powerwash when post-install
58// completes successfully so that the device is powerwashed on next reboot.
59const char kPowerwashMarkerFile[] =
60 "/mnt/stateful_partition/factory_install_reset";
61
62// The contents of the powerwash marker file.
63const char kPowerwashCommand[] = "safe fast keepimg reason=update_engine\n";
64
Alex Deymo46a9aae2016-05-04 20:20:11 -070065// UpdateManager config path.
66const char* kConfigFilePath = "/etc/update_manager.conf";
67
68// UpdateManager config options:
69const char* kConfigOptsIsOOBEEnabled = "is_oobe_enabled";
70
Amin Hassani1677e812017-06-21 13:36:36 -070071const char* kActivePingKey = "first_active_omaha_ping_sent";
72
Alex Deymobccbc382014-04-03 13:38:55 -070073} // namespace
74
Alex Deymo42432912013-07-12 20:21:15 -070075namespace chromeos_update_engine {
76
Alex Deymo40d86b22015-09-03 22:27:10 -070077namespace hardware {
78
79// Factory defined in hardware.h.
80std::unique_ptr<HardwareInterface> CreateHardware() {
Alex Deymo46a9aae2016-05-04 20:20:11 -070081 std::unique_ptr<HardwareChromeOS> hardware(new HardwareChromeOS());
82 hardware->Init();
83 return std::move(hardware);
Alex Deymo40d86b22015-09-03 22:27:10 -070084}
85
86} // namespace hardware
87
Alex Deymo46a9aae2016-05-04 20:20:11 -070088void HardwareChromeOS::Init() {
89 LoadConfig("" /* root_prefix */, IsNormalBootMode());
Sen Jiange67bb5b2016-06-20 15:53:56 -070090 debugd_proxy_.reset(
91 new org::chromium::debugdProxy(DBusConnection::Get()->GetDBus()));
Alex Deymo46a9aae2016-05-04 20:20:11 -070092}
93
Alex Deymo40d86b22015-09-03 22:27:10 -070094bool HardwareChromeOS::IsOfficialBuild() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070095 return VbGetSystemPropertyInt("debug_build") == 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -070096}
97
Alex Deymo40d86b22015-09-03 22:27:10 -070098bool HardwareChromeOS::IsNormalBootMode() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070099 bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700100 return !dev_mode;
101}
102
Sen Jiange67bb5b2016-06-20 15:53:56 -0700103bool HardwareChromeOS::AreDevFeaturesEnabled() const {
104 // Even though the debugd tools are also gated on devmode, checking here can
105 // save us a D-Bus call so it's worth doing explicitly.
106 if (IsNormalBootMode())
107 return false;
108
109 int32_t dev_features = debugd::DEV_FEATURES_DISABLED;
110 brillo::ErrorPtr error;
111 // Some boards may not include debugd so it's expected that this may fail,
112 // in which case we treat it as disabled.
113 if (debugd_proxy_ && debugd_proxy_->QueryDevFeatures(&dev_features, &error) &&
114 !(dev_features & debugd::DEV_FEATURES_DISABLED)) {
115 LOG(INFO) << "Debugd dev tools enabled.";
116 return true;
117 }
118 return false;
119}
120
Alex Deymo46a9aae2016-05-04 20:20:11 -0700121bool HardwareChromeOS::IsOOBEEnabled() const {
122 return is_oobe_enabled_;
123}
124
Alex Deymo40d86b22015-09-03 22:27:10 -0700125bool HardwareChromeOS::IsOOBEComplete(base::Time* out_time_of_oobe) const {
Alex Deymo46a9aae2016-05-04 20:20:11 -0700126 if (!is_oobe_enabled_) {
127 LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() was called";
128 }
Alex Deymobccbc382014-04-03 13:38:55 -0700129 struct stat statbuf;
130 if (stat(kOOBECompletedMarker, &statbuf) != 0) {
131 if (errno != ENOENT) {
132 PLOG(ERROR) << "Error getting information about "
133 << kOOBECompletedMarker;
134 }
135 return false;
136 }
137
138 if (out_time_of_oobe != nullptr)
139 *out_time_of_oobe = base::Time::FromTimeT(statbuf.st_mtime);
140 return true;
141}
142
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700143static string ReadValueFromCrosSystem(const string& key) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700144 char value_buffer[VB_MAX_STRING_PROPERTY];
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700145
Alex Deymo40d86b22015-09-03 22:27:10 -0700146 const char* rv = VbGetSystemPropertyString(key.c_str(), value_buffer,
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700147 sizeof(value_buffer));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700148 if (rv != nullptr) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700149 string return_value(value_buffer);
Ben Chan736fcb52014-05-21 18:28:22 -0700150 base::TrimWhitespaceASCII(return_value, base::TRIM_ALL, &return_value);
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700151 return return_value;
152 }
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700153
154 LOG(ERROR) << "Unable to read crossystem key " << key;
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700155 return "";
156}
157
Alex Deymo40d86b22015-09-03 22:27:10 -0700158string HardwareChromeOS::GetHardwareClass() const {
Chris Masonef8d037f2014-02-19 01:53:00 +0000159 if (USE_HWID_OVERRIDE) {
160 return HwidOverride::Read(base::FilePath("/"));
161 }
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700162 return ReadValueFromCrosSystem("hwid");
163}
164
Alex Deymo40d86b22015-09-03 22:27:10 -0700165string HardwareChromeOS::GetFirmwareVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700166 return ReadValueFromCrosSystem("fwid");
167}
168
Alex Deymo40d86b22015-09-03 22:27:10 -0700169string HardwareChromeOS::GetECVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700170 string input_line;
171 int exit_code = 0;
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800172 vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"};
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700173
174 bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line);
175 if (!success || exit_code) {
176 LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")";
177 return "";
178 }
179
180 return utils::ParseECVersion(input_line);
181}
182
Alex Deymo40d86b22015-09-03 22:27:10 -0700183int HardwareChromeOS::GetPowerwashCount() const {
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700184 int powerwash_count;
Alex Deymodd132f32015-09-14 19:12:07 -0700185 base::FilePath marker_path = base::FilePath(kPowerwashSafeDirectory).Append(
186 kPowerwashCountMarker);
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700187 string contents;
Alex Deymodd132f32015-09-14 19:12:07 -0700188 if (!utils::ReadFile(marker_path.value(), &contents))
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700189 return -1;
190 base::TrimWhitespaceASCII(contents, base::TRIM_TRAILING, &contents);
191 if (!base::StringToInt(contents, &powerwash_count))
192 return -1;
193 return powerwash_count;
194}
195
Alex Deymofb905d92016-06-03 19:26:58 -0700196bool HardwareChromeOS::SchedulePowerwash() {
197 bool result = utils::WriteFile(
198 kPowerwashMarkerFile, kPowerwashCommand, strlen(kPowerwashCommand));
199 if (result) {
Alex Deymocaa46722016-06-09 12:08:29 -0700200 LOG(INFO) << "Created " << kPowerwashMarkerFile
201 << " to powerwash on next reboot";
Alex Deymofb905d92016-06-03 19:26:58 -0700202 } else {
Alex Deymocaa46722016-06-09 12:08:29 -0700203 PLOG(ERROR) << "Error in creating powerwash marker file: "
204 << kPowerwashMarkerFile;
Alex Deymofb905d92016-06-03 19:26:58 -0700205 }
206
207 return result;
208}
209
210bool HardwareChromeOS::CancelPowerwash() {
211 bool result = base::DeleteFile(base::FilePath(kPowerwashMarkerFile), false);
212
213 if (result) {
214 LOG(INFO) << "Successfully deleted the powerwash marker file : "
Alex Deymocaa46722016-06-09 12:08:29 -0700215 << kPowerwashMarkerFile;
Alex Deymofb905d92016-06-03 19:26:58 -0700216 } else {
217 PLOG(ERROR) << "Could not delete the powerwash marker file : "
Alex Deymocaa46722016-06-09 12:08:29 -0700218 << kPowerwashMarkerFile;
Alex Deymofb905d92016-06-03 19:26:58 -0700219 }
220
221 return result;
222}
223
Alex Deymodd132f32015-09-14 19:12:07 -0700224bool HardwareChromeOS::GetNonVolatileDirectory(base::FilePath* path) const {
Sen Jiang9c123462015-11-19 13:16:23 -0800225 *path = base::FilePath(constants::kNonVolatileDirectory);
Alex Deymodd132f32015-09-14 19:12:07 -0700226 return true;
227}
228
229bool HardwareChromeOS::GetPowerwashSafeDirectory(base::FilePath* path) const {
230 *path = base::FilePath(kPowerwashSafeDirectory);
231 return true;
232}
233
Alex Deymo46a9aae2016-05-04 20:20:11 -0700234void HardwareChromeOS::LoadConfig(const string& root_prefix, bool normal_mode) {
235 brillo::KeyValueStore store;
236
237 if (normal_mode) {
238 store.Load(base::FilePath(root_prefix + kConfigFilePath));
239 } else {
240 if (store.Load(base::FilePath(root_prefix + kStatefulPartition +
241 kConfigFilePath))) {
242 LOG(INFO) << "UpdateManager Config loaded from stateful partition.";
243 } else {
244 store.Load(base::FilePath(root_prefix + kConfigFilePath));
245 }
246 }
247
248 if (!store.GetBoolean(kConfigOptsIsOOBEEnabled, &is_oobe_enabled_))
249 is_oobe_enabled_ = true; // Default value.
250}
251
Amin Hassani1677e812017-06-21 13:36:36 -0700252bool HardwareChromeOS::GetFirstActiveOmahaPingSent() const {
253 int exit_code = 0;
254 string active_ping_str;
255 vector<string> cmd = { "vpd_get_value", kActivePingKey };
256 if (!Subprocess::SynchronousExec(cmd, &exit_code, &active_ping_str) ||
257 exit_code) {
258 LOG(ERROR) << "Failed to get vpd key for " << kActivePingKey
259 << " with exit code: " << exit_code;
260 return false;
261 }
262
263 base::TrimWhitespaceASCII(active_ping_str,
264 base::TRIM_ALL,
265 &active_ping_str);
266 int active_ping;
267 if (active_ping_str.empty() ||
268 !base::StringToInt(active_ping_str, &active_ping)) {
269 LOG(INFO) << "Failed to parse active_ping value: " << active_ping_str;
270 return false;
271 }
272 return static_cast<bool>(active_ping);
273}
274
275void HardwareChromeOS::SetFirstActiveOmahaPingSent() {
276 int exit_code = 0;
277 string output;
278 vector<string> vpd_set_cmd = {
279 "vpd", "-i", "RW_VPD", "-s", string(kActivePingKey) + "=1" };
280 if (!Subprocess::SynchronousExec(vpd_set_cmd, &exit_code, &output) ||
281 exit_code) {
282 LOG(ERROR) << "Failed to set vpd key for " << kActivePingKey
283 << " with exit code: " << exit_code
284 << " with error: " << output;
285 return;
286 }
287
288 vector<string> vpd_dump_cmd = { "dump_vpd_log", "--force" };
289 if (!Subprocess::SynchronousExec(vpd_dump_cmd, &exit_code, &output) ||
290 exit_code) {
291 LOG(ERROR) << "Failed to cache " << kActivePingKey<< " using dump_vpd_log"
292 << " with exit code: " << exit_code
293 << " with error: " << output;
294 }
295}
296
Alex Deymo42432912013-07-12 20:21:15 -0700297} // namespace chromeos_update_engine