blob: 4b0b82f323c7d6a57cb0a005894ba0f649bf245c [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
Alex Deymobccbc382014-04-03 13:38:55 -070072} // namespace
73
Alex Deymo42432912013-07-12 20:21:15 -070074namespace chromeos_update_engine {
75
Alex Deymo40d86b22015-09-03 22:27:10 -070076namespace hardware {
77
78// Factory defined in hardware.h.
79std::unique_ptr<HardwareInterface> CreateHardware() {
Alex Deymo46a9aae2016-05-04 20:20:11 -070080 std::unique_ptr<HardwareChromeOS> hardware(new HardwareChromeOS());
81 hardware->Init();
82 return std::move(hardware);
Alex Deymo40d86b22015-09-03 22:27:10 -070083}
84
85} // namespace hardware
86
Alex Deymo46a9aae2016-05-04 20:20:11 -070087void HardwareChromeOS::Init() {
88 LoadConfig("" /* root_prefix */, IsNormalBootMode());
Sen Jiange67bb5b2016-06-20 15:53:56 -070089 debugd_proxy_.reset(
90 new org::chromium::debugdProxy(DBusConnection::Get()->GetDBus()));
Alex Deymo46a9aae2016-05-04 20:20:11 -070091}
92
Alex Deymo40d86b22015-09-03 22:27:10 -070093bool HardwareChromeOS::IsOfficialBuild() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070094 return VbGetSystemPropertyInt("debug_build") == 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -070095}
96
Alex Deymo40d86b22015-09-03 22:27:10 -070097bool HardwareChromeOS::IsNormalBootMode() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070098 bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -070099 return !dev_mode;
100}
101
Sen Jiange67bb5b2016-06-20 15:53:56 -0700102bool HardwareChromeOS::AreDevFeaturesEnabled() const {
103 // Even though the debugd tools are also gated on devmode, checking here can
104 // save us a D-Bus call so it's worth doing explicitly.
105 if (IsNormalBootMode())
106 return false;
107
108 int32_t dev_features = debugd::DEV_FEATURES_DISABLED;
109 brillo::ErrorPtr error;
110 // Some boards may not include debugd so it's expected that this may fail,
111 // in which case we treat it as disabled.
112 if (debugd_proxy_ && debugd_proxy_->QueryDevFeatures(&dev_features, &error) &&
113 !(dev_features & debugd::DEV_FEATURES_DISABLED)) {
114 LOG(INFO) << "Debugd dev tools enabled.";
115 return true;
116 }
117 return false;
118}
119
Alex Deymo46a9aae2016-05-04 20:20:11 -0700120bool HardwareChromeOS::IsOOBEEnabled() const {
121 return is_oobe_enabled_;
122}
123
Alex Deymo40d86b22015-09-03 22:27:10 -0700124bool HardwareChromeOS::IsOOBEComplete(base::Time* out_time_of_oobe) const {
Alex Deymo46a9aae2016-05-04 20:20:11 -0700125 if (!is_oobe_enabled_) {
126 LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() was called";
127 }
Alex Deymobccbc382014-04-03 13:38:55 -0700128 struct stat statbuf;
129 if (stat(kOOBECompletedMarker, &statbuf) != 0) {
130 if (errno != ENOENT) {
131 PLOG(ERROR) << "Error getting information about "
132 << kOOBECompletedMarker;
133 }
134 return false;
135 }
136
137 if (out_time_of_oobe != nullptr)
138 *out_time_of_oobe = base::Time::FromTimeT(statbuf.st_mtime);
139 return true;
140}
141
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700142static string ReadValueFromCrosSystem(const string& key) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700143 char value_buffer[VB_MAX_STRING_PROPERTY];
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700144
Alex Deymo40d86b22015-09-03 22:27:10 -0700145 const char* rv = VbGetSystemPropertyString(key.c_str(), value_buffer,
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700146 sizeof(value_buffer));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700147 if (rv != nullptr) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700148 string return_value(value_buffer);
Ben Chan736fcb52014-05-21 18:28:22 -0700149 base::TrimWhitespaceASCII(return_value, base::TRIM_ALL, &return_value);
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700150 return return_value;
151 }
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700152
153 LOG(ERROR) << "Unable to read crossystem key " << key;
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700154 return "";
155}
156
Alex Deymo40d86b22015-09-03 22:27:10 -0700157string HardwareChromeOS::GetHardwareClass() const {
Chris Masonef8d037f2014-02-19 01:53:00 +0000158 if (USE_HWID_OVERRIDE) {
159 return HwidOverride::Read(base::FilePath("/"));
160 }
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700161 return ReadValueFromCrosSystem("hwid");
162}
163
Alex Deymo40d86b22015-09-03 22:27:10 -0700164string HardwareChromeOS::GetFirmwareVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700165 return ReadValueFromCrosSystem("fwid");
166}
167
Alex Deymo40d86b22015-09-03 22:27:10 -0700168string HardwareChromeOS::GetECVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700169 string input_line;
170 int exit_code = 0;
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800171 vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"};
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700172
173 bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line);
174 if (!success || exit_code) {
175 LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")";
176 return "";
177 }
178
179 return utils::ParseECVersion(input_line);
180}
181
Alex Deymo40d86b22015-09-03 22:27:10 -0700182int HardwareChromeOS::GetPowerwashCount() const {
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700183 int powerwash_count;
Alex Deymodd132f32015-09-14 19:12:07 -0700184 base::FilePath marker_path = base::FilePath(kPowerwashSafeDirectory).Append(
185 kPowerwashCountMarker);
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700186 string contents;
Alex Deymodd132f32015-09-14 19:12:07 -0700187 if (!utils::ReadFile(marker_path.value(), &contents))
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700188 return -1;
189 base::TrimWhitespaceASCII(contents, base::TRIM_TRAILING, &contents);
190 if (!base::StringToInt(contents, &powerwash_count))
191 return -1;
192 return powerwash_count;
193}
194
Alex Deymofb905d92016-06-03 19:26:58 -0700195bool HardwareChromeOS::SchedulePowerwash() {
196 bool result = utils::WriteFile(
197 kPowerwashMarkerFile, kPowerwashCommand, strlen(kPowerwashCommand));
198 if (result) {
Alex Deymocaa46722016-06-09 12:08:29 -0700199 LOG(INFO) << "Created " << kPowerwashMarkerFile
200 << " to powerwash on next reboot";
Alex Deymofb905d92016-06-03 19:26:58 -0700201 } else {
Alex Deymocaa46722016-06-09 12:08:29 -0700202 PLOG(ERROR) << "Error in creating powerwash marker file: "
203 << kPowerwashMarkerFile;
Alex Deymofb905d92016-06-03 19:26:58 -0700204 }
205
206 return result;
207}
208
209bool HardwareChromeOS::CancelPowerwash() {
210 bool result = base::DeleteFile(base::FilePath(kPowerwashMarkerFile), false);
211
212 if (result) {
213 LOG(INFO) << "Successfully deleted the powerwash marker file : "
Alex Deymocaa46722016-06-09 12:08:29 -0700214 << kPowerwashMarkerFile;
Alex Deymofb905d92016-06-03 19:26:58 -0700215 } else {
216 PLOG(ERROR) << "Could not delete the powerwash marker file : "
Alex Deymocaa46722016-06-09 12:08:29 -0700217 << kPowerwashMarkerFile;
Alex Deymofb905d92016-06-03 19:26:58 -0700218 }
219
220 return result;
221}
222
Alex Deymodd132f32015-09-14 19:12:07 -0700223bool HardwareChromeOS::GetNonVolatileDirectory(base::FilePath* path) const {
Sen Jiang9c123462015-11-19 13:16:23 -0800224 *path = base::FilePath(constants::kNonVolatileDirectory);
Alex Deymodd132f32015-09-14 19:12:07 -0700225 return true;
226}
227
228bool HardwareChromeOS::GetPowerwashSafeDirectory(base::FilePath* path) const {
229 *path = base::FilePath(kPowerwashSafeDirectory);
230 return true;
231}
232
Alex Deymo46a9aae2016-05-04 20:20:11 -0700233void HardwareChromeOS::LoadConfig(const string& root_prefix, bool normal_mode) {
234 brillo::KeyValueStore store;
235
236 if (normal_mode) {
237 store.Load(base::FilePath(root_prefix + kConfigFilePath));
238 } else {
239 if (store.Load(base::FilePath(root_prefix + kStatefulPartition +
240 kConfigFilePath))) {
241 LOG(INFO) << "UpdateManager Config loaded from stateful partition.";
242 } else {
243 store.Load(base::FilePath(root_prefix + kConfigFilePath));
244 }
245 }
246
247 if (!store.GetBoolean(kConfigOptsIsOOBEEnabled, &is_oobe_enabled_))
248 is_oobe_enabled_ = true; // Default value.
249}
250
Alex Deymo42432912013-07-12 20:21:15 -0700251} // namespace chromeos_update_engine