blob: 47beea8882d6e271848ba4cabc5a9a97f1d941bb [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>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070025#include <brillo/make_unique_ptr.h>
Sen Jiange67bb5b2016-06-20 15:53:56 -070026#include <debugd/dbus-constants.h>
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070027#include <vboot/crossystem.h>
Alex Deymo42432912013-07-12 20:21:15 -070028
Don Garrett83692e42013-11-08 10:11:30 -080029extern "C" {
30#include "vboot/vboot_host.h"
31}
32
Alex Deymo46a9aae2016-05-04 20:20:11 -070033#include "update_engine/common/constants.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080034#include "update_engine/common/hardware.h"
35#include "update_engine/common/hwid_override.h"
Sen Jiang9c123462015-11-19 13:16:23 -080036#include "update_engine/common/platform_constants.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080037#include "update_engine/common/subprocess.h"
38#include "update_engine/common/utils.h"
Sen Jiange67bb5b2016-06-20 15:53:56 -070039#include "update_engine/dbus_connection.h"
J. Richard Barnette522d36f2013-10-28 17:22:12 -070040
Alex Deymo42432912013-07-12 20:21:15 -070041using std::string;
J. Richard Barnette522d36f2013-10-28 17:22:12 -070042using std::vector;
Alex Deymo42432912013-07-12 20:21:15 -070043
Alex Deymobccbc382014-04-03 13:38:55 -070044namespace {
45
Alex Deymodd132f32015-09-14 19:12:07 -070046const char kOOBECompletedMarker[] = "/home/chronos/.oobe_completed";
47
48// The stateful directory used by update_engine to store powerwash-safe files.
49// The files stored here must be whitelisted in the powerwash scripts.
50const char kPowerwashSafeDirectory[] =
51 "/mnt/stateful_partition/unencrypted/preserve";
Alex Deymobccbc382014-04-03 13:38:55 -070052
Alex Deymoebbe7ef2014-10-30 13:02:49 -070053// The powerwash_count marker file contains the number of times the device was
54// powerwashed. This value is incremented by the clobber-state script when
55// a powerwash is performed.
Alex Deymodd132f32015-09-14 19:12:07 -070056const char kPowerwashCountMarker[] = "powerwash_count";
57
Alex Deymofb905d92016-06-03 19:26:58 -070058// The name of the marker file used to trigger powerwash when post-install
59// completes successfully so that the device is powerwashed on next reboot.
60const char kPowerwashMarkerFile[] =
61 "/mnt/stateful_partition/factory_install_reset";
62
63// The contents of the powerwash marker file.
64const char kPowerwashCommand[] = "safe fast keepimg reason=update_engine\n";
65
Alex Deymo46a9aae2016-05-04 20:20:11 -070066// UpdateManager config path.
67const char* kConfigFilePath = "/etc/update_manager.conf";
68
69// UpdateManager config options:
70const char* kConfigOptsIsOOBEEnabled = "is_oobe_enabled";
71
Amin Hassani1677e812017-06-21 13:36:36 -070072const char* kActivePingKey = "first_active_omaha_ping_sent";
73
Alex Deymobccbc382014-04-03 13:38:55 -070074} // namespace
75
Alex Deymo42432912013-07-12 20:21:15 -070076namespace chromeos_update_engine {
77
Alex Deymo40d86b22015-09-03 22:27:10 -070078namespace hardware {
79
80// Factory defined in hardware.h.
81std::unique_ptr<HardwareInterface> CreateHardware() {
Alex Deymo46a9aae2016-05-04 20:20:11 -070082 std::unique_ptr<HardwareChromeOS> hardware(new HardwareChromeOS());
83 hardware->Init();
84 return std::move(hardware);
Alex Deymo40d86b22015-09-03 22:27:10 -070085}
86
87} // namespace hardware
88
Alex Deymo46a9aae2016-05-04 20:20:11 -070089void HardwareChromeOS::Init() {
90 LoadConfig("" /* root_prefix */, IsNormalBootMode());
Sen Jiange67bb5b2016-06-20 15:53:56 -070091 debugd_proxy_.reset(
92 new org::chromium::debugdProxy(DBusConnection::Get()->GetDBus()));
Alex Deymo46a9aae2016-05-04 20:20:11 -070093}
94
Alex Deymo40d86b22015-09-03 22:27:10 -070095bool HardwareChromeOS::IsOfficialBuild() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070096 return VbGetSystemPropertyInt("debug_build") == 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -070097}
98
Alex Deymo40d86b22015-09-03 22:27:10 -070099bool HardwareChromeOS::IsNormalBootMode() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700100 bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700101 return !dev_mode;
102}
103
Sen Jiange67bb5b2016-06-20 15:53:56 -0700104bool HardwareChromeOS::AreDevFeaturesEnabled() const {
105 // Even though the debugd tools are also gated on devmode, checking here can
106 // save us a D-Bus call so it's worth doing explicitly.
107 if (IsNormalBootMode())
108 return false;
109
110 int32_t dev_features = debugd::DEV_FEATURES_DISABLED;
111 brillo::ErrorPtr error;
112 // Some boards may not include debugd so it's expected that this may fail,
113 // in which case we treat it as disabled.
114 if (debugd_proxy_ && debugd_proxy_->QueryDevFeatures(&dev_features, &error) &&
115 !(dev_features & debugd::DEV_FEATURES_DISABLED)) {
116 LOG(INFO) << "Debugd dev tools enabled.";
117 return true;
118 }
119 return false;
120}
121
Alex Deymo46a9aae2016-05-04 20:20:11 -0700122bool HardwareChromeOS::IsOOBEEnabled() const {
123 return is_oobe_enabled_;
124}
125
Alex Deymo40d86b22015-09-03 22:27:10 -0700126bool HardwareChromeOS::IsOOBEComplete(base::Time* out_time_of_oobe) const {
Alex Deymo46a9aae2016-05-04 20:20:11 -0700127 if (!is_oobe_enabled_) {
128 LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() was called";
129 }
Alex Deymobccbc382014-04-03 13:38:55 -0700130 struct stat statbuf;
131 if (stat(kOOBECompletedMarker, &statbuf) != 0) {
132 if (errno != ENOENT) {
133 PLOG(ERROR) << "Error getting information about "
134 << kOOBECompletedMarker;
135 }
136 return false;
137 }
138
139 if (out_time_of_oobe != nullptr)
140 *out_time_of_oobe = base::Time::FromTimeT(statbuf.st_mtime);
141 return true;
142}
143
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700144static string ReadValueFromCrosSystem(const string& key) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700145 char value_buffer[VB_MAX_STRING_PROPERTY];
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700146
Alex Deymo40d86b22015-09-03 22:27:10 -0700147 const char* rv = VbGetSystemPropertyString(key.c_str(), value_buffer,
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700148 sizeof(value_buffer));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700149 if (rv != nullptr) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700150 string return_value(value_buffer);
Ben Chan736fcb52014-05-21 18:28:22 -0700151 base::TrimWhitespaceASCII(return_value, base::TRIM_ALL, &return_value);
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700152 return return_value;
153 }
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700154
155 LOG(ERROR) << "Unable to read crossystem key " << key;
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700156 return "";
157}
158
Alex Deymo40d86b22015-09-03 22:27:10 -0700159string HardwareChromeOS::GetHardwareClass() const {
Chris Masonef8d037f2014-02-19 01:53:00 +0000160 if (USE_HWID_OVERRIDE) {
161 return HwidOverride::Read(base::FilePath("/"));
162 }
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700163 return ReadValueFromCrosSystem("hwid");
164}
165
Alex Deymo40d86b22015-09-03 22:27:10 -0700166string HardwareChromeOS::GetFirmwareVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700167 return ReadValueFromCrosSystem("fwid");
168}
169
Alex Deymo40d86b22015-09-03 22:27:10 -0700170string HardwareChromeOS::GetECVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700171 string input_line;
172 int exit_code = 0;
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800173 vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"};
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700174
175 bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line);
176 if (!success || exit_code) {
177 LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")";
178 return "";
179 }
180
181 return utils::ParseECVersion(input_line);
182}
183
Alex Deymo40d86b22015-09-03 22:27:10 -0700184int HardwareChromeOS::GetPowerwashCount() const {
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700185 int powerwash_count;
Alex Deymodd132f32015-09-14 19:12:07 -0700186 base::FilePath marker_path = base::FilePath(kPowerwashSafeDirectory).Append(
187 kPowerwashCountMarker);
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700188 string contents;
Alex Deymodd132f32015-09-14 19:12:07 -0700189 if (!utils::ReadFile(marker_path.value(), &contents))
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700190 return -1;
191 base::TrimWhitespaceASCII(contents, base::TRIM_TRAILING, &contents);
192 if (!base::StringToInt(contents, &powerwash_count))
193 return -1;
194 return powerwash_count;
195}
196
Alex Deymofb905d92016-06-03 19:26:58 -0700197bool HardwareChromeOS::SchedulePowerwash() {
198 bool result = utils::WriteFile(
199 kPowerwashMarkerFile, kPowerwashCommand, strlen(kPowerwashCommand));
200 if (result) {
Alex Deymocaa46722016-06-09 12:08:29 -0700201 LOG(INFO) << "Created " << kPowerwashMarkerFile
202 << " to powerwash on next reboot";
Alex Deymofb905d92016-06-03 19:26:58 -0700203 } else {
Alex Deymocaa46722016-06-09 12:08:29 -0700204 PLOG(ERROR) << "Error in creating powerwash marker file: "
205 << kPowerwashMarkerFile;
Alex Deymofb905d92016-06-03 19:26:58 -0700206 }
207
208 return result;
209}
210
211bool HardwareChromeOS::CancelPowerwash() {
212 bool result = base::DeleteFile(base::FilePath(kPowerwashMarkerFile), false);
213
214 if (result) {
215 LOG(INFO) << "Successfully deleted the powerwash marker file : "
Alex Deymocaa46722016-06-09 12:08:29 -0700216 << kPowerwashMarkerFile;
Alex Deymofb905d92016-06-03 19:26:58 -0700217 } else {
218 PLOG(ERROR) << "Could not delete the powerwash marker file : "
Alex Deymocaa46722016-06-09 12:08:29 -0700219 << kPowerwashMarkerFile;
Alex Deymofb905d92016-06-03 19:26:58 -0700220 }
221
222 return result;
223}
224
Alex Deymodd132f32015-09-14 19:12:07 -0700225bool HardwareChromeOS::GetNonVolatileDirectory(base::FilePath* path) const {
Sen Jiang9c123462015-11-19 13:16:23 -0800226 *path = base::FilePath(constants::kNonVolatileDirectory);
Alex Deymodd132f32015-09-14 19:12:07 -0700227 return true;
228}
229
230bool HardwareChromeOS::GetPowerwashSafeDirectory(base::FilePath* path) const {
231 *path = base::FilePath(kPowerwashSafeDirectory);
232 return true;
233}
234
Alex Deymo46a9aae2016-05-04 20:20:11 -0700235void HardwareChromeOS::LoadConfig(const string& root_prefix, bool normal_mode) {
236 brillo::KeyValueStore store;
237
238 if (normal_mode) {
239 store.Load(base::FilePath(root_prefix + kConfigFilePath));
240 } else {
241 if (store.Load(base::FilePath(root_prefix + kStatefulPartition +
242 kConfigFilePath))) {
243 LOG(INFO) << "UpdateManager Config loaded from stateful partition.";
244 } else {
245 store.Load(base::FilePath(root_prefix + kConfigFilePath));
246 }
247 }
248
249 if (!store.GetBoolean(kConfigOptsIsOOBEEnabled, &is_oobe_enabled_))
250 is_oobe_enabled_ = true; // Default value.
251}
252
Amin Hassani1677e812017-06-21 13:36:36 -0700253bool HardwareChromeOS::GetFirstActiveOmahaPingSent() const {
254 int exit_code = 0;
255 string active_ping_str;
256 vector<string> cmd = { "vpd_get_value", kActivePingKey };
257 if (!Subprocess::SynchronousExec(cmd, &exit_code, &active_ping_str) ||
258 exit_code) {
259 LOG(ERROR) << "Failed to get vpd key for " << kActivePingKey
260 << " with exit code: " << exit_code;
261 return false;
262 }
263
264 base::TrimWhitespaceASCII(active_ping_str,
265 base::TRIM_ALL,
266 &active_ping_str);
267 int active_ping;
268 if (active_ping_str.empty() ||
269 !base::StringToInt(active_ping_str, &active_ping)) {
270 LOG(INFO) << "Failed to parse active_ping value: " << active_ping_str;
271 return false;
272 }
273 return static_cast<bool>(active_ping);
274}
275
276void HardwareChromeOS::SetFirstActiveOmahaPingSent() {
277 int exit_code = 0;
278 string output;
279 vector<string> vpd_set_cmd = {
280 "vpd", "-i", "RW_VPD", "-s", string(kActivePingKey) + "=1" };
281 if (!Subprocess::SynchronousExec(vpd_set_cmd, &exit_code, &output) ||
282 exit_code) {
283 LOG(ERROR) << "Failed to set vpd key for " << kActivePingKey
284 << " with exit code: " << exit_code
285 << " with error: " << output;
286 return;
287 }
288
289 vector<string> vpd_dump_cmd = { "dump_vpd_log", "--force" };
290 if (!Subprocess::SynchronousExec(vpd_dump_cmd, &exit_code, &output) ||
291 exit_code) {
292 LOG(ERROR) << "Failed to cache " << kActivePingKey<< " using dump_vpd_log"
293 << " with exit code: " << exit_code
294 << " with error: " << output;
295 }
296}
297
Alex Deymo42432912013-07-12 20:21:15 -0700298} // namespace chromeos_update_engine