Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 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 Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 16 | |
Alex Deymo | 1b03f9f | 2015-12-09 00:38:36 -0800 | [diff] [blame] | 17 | #include "update_engine/hardware_chromeos.h" |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 18 | |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 19 | #include <base/files/file_path.h> |
Ben Chan | 06c76a4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 20 | #include <base/files/file_util.h> |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 21 | #include <base/logging.h> |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 22 | #include <base/strings/string_number_conversions.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 23 | #include <base/strings/string_util.h> |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 24 | #include <brillo/key_value_store.h> |
Sen Jiang | e67bb5b | 2016-06-20 15:53:56 -0700 | [diff] [blame] | 25 | #include <debugd/dbus-constants.h> |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 26 | #include <vboot/crossystem.h> |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 27 | |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 28 | extern "C" { |
| 29 | #include "vboot/vboot_host.h" |
| 30 | } |
| 31 | |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 32 | #include "update_engine/common/constants.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 33 | #include "update_engine/common/hardware.h" |
| 34 | #include "update_engine/common/hwid_override.h" |
Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 35 | #include "update_engine/common/platform_constants.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 36 | #include "update_engine/common/subprocess.h" |
| 37 | #include "update_engine/common/utils.h" |
Sen Jiang | e67bb5b | 2016-06-20 15:53:56 -0700 | [diff] [blame] | 38 | #include "update_engine/dbus_connection.h" |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 39 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 40 | using std::string; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 41 | using std::vector; |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 42 | |
Alex Deymo | bccbc38 | 2014-04-03 13:38:55 -0700 | [diff] [blame] | 43 | namespace { |
| 44 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 45 | const 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. |
| 49 | const char kPowerwashSafeDirectory[] = |
| 50 | "/mnt/stateful_partition/unencrypted/preserve"; |
Alex Deymo | bccbc38 | 2014-04-03 13:38:55 -0700 | [diff] [blame] | 51 | |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 52 | // 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 Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 55 | const char kPowerwashCountMarker[] = "powerwash_count"; |
| 56 | |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 57 | // 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. |
| 59 | const char kPowerwashMarkerFile[] = |
| 60 | "/mnt/stateful_partition/factory_install_reset"; |
| 61 | |
| 62 | // The contents of the powerwash marker file. |
| 63 | const char kPowerwashCommand[] = "safe fast keepimg reason=update_engine\n"; |
| 64 | |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 65 | // UpdateManager config path. |
| 66 | const char* kConfigFilePath = "/etc/update_manager.conf"; |
| 67 | |
| 68 | // UpdateManager config options: |
| 69 | const char* kConfigOptsIsOOBEEnabled = "is_oobe_enabled"; |
| 70 | |
Amin Hassani | 1677e81 | 2017-06-21 13:36:36 -0700 | [diff] [blame] | 71 | const char* kActivePingKey = "first_active_omaha_ping_sent"; |
| 72 | |
Alex Deymo | bccbc38 | 2014-04-03 13:38:55 -0700 | [diff] [blame] | 73 | } // namespace |
| 74 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 75 | namespace chromeos_update_engine { |
| 76 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 77 | namespace hardware { |
| 78 | |
| 79 | // Factory defined in hardware.h. |
| 80 | std::unique_ptr<HardwareInterface> CreateHardware() { |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 81 | std::unique_ptr<HardwareChromeOS> hardware(new HardwareChromeOS()); |
| 82 | hardware->Init(); |
| 83 | return std::move(hardware); |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | } // namespace hardware |
| 87 | |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 88 | void HardwareChromeOS::Init() { |
| 89 | LoadConfig("" /* root_prefix */, IsNormalBootMode()); |
Sen Jiang | e67bb5b | 2016-06-20 15:53:56 -0700 | [diff] [blame] | 90 | debugd_proxy_.reset( |
| 91 | new org::chromium::debugdProxy(DBusConnection::Get()->GetDBus())); |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 94 | bool HardwareChromeOS::IsOfficialBuild() const { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 95 | return VbGetSystemPropertyInt("debug_build") == 0; |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 98 | bool HardwareChromeOS::IsNormalBootMode() const { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 99 | bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0; |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 100 | return !dev_mode; |
| 101 | } |
| 102 | |
Sen Jiang | e67bb5b | 2016-06-20 15:53:56 -0700 | [diff] [blame] | 103 | bool 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 Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 121 | bool HardwareChromeOS::IsOOBEEnabled() const { |
| 122 | return is_oobe_enabled_; |
| 123 | } |
| 124 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 125 | bool HardwareChromeOS::IsOOBEComplete(base::Time* out_time_of_oobe) const { |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 126 | if (!is_oobe_enabled_) { |
| 127 | LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() was called"; |
| 128 | } |
Alex Deymo | bccbc38 | 2014-04-03 13:38:55 -0700 | [diff] [blame] | 129 | 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 Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 143 | static string ReadValueFromCrosSystem(const string& key) { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 144 | char value_buffer[VB_MAX_STRING_PROPERTY]; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 145 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 146 | const char* rv = VbGetSystemPropertyString(key.c_str(), value_buffer, |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 147 | sizeof(value_buffer)); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 148 | if (rv != nullptr) { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 149 | string return_value(value_buffer); |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 150 | base::TrimWhitespaceASCII(return_value, base::TRIM_ALL, &return_value); |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 151 | return return_value; |
| 152 | } |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 153 | |
| 154 | LOG(ERROR) << "Unable to read crossystem key " << key; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 155 | return ""; |
| 156 | } |
| 157 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 158 | string HardwareChromeOS::GetHardwareClass() const { |
Chris Masone | f8d037f | 2014-02-19 01:53:00 +0000 | [diff] [blame] | 159 | if (USE_HWID_OVERRIDE) { |
| 160 | return HwidOverride::Read(base::FilePath("/")); |
| 161 | } |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 162 | return ReadValueFromCrosSystem("hwid"); |
| 163 | } |
| 164 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 165 | string HardwareChromeOS::GetFirmwareVersion() const { |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 166 | return ReadValueFromCrosSystem("fwid"); |
| 167 | } |
| 168 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 169 | string HardwareChromeOS::GetECVersion() const { |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 170 | string input_line; |
| 171 | int exit_code = 0; |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 172 | vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"}; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 173 | |
| 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 Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 183 | int HardwareChromeOS::GetPowerwashCount() const { |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 184 | int powerwash_count; |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 185 | base::FilePath marker_path = base::FilePath(kPowerwashSafeDirectory).Append( |
| 186 | kPowerwashCountMarker); |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 187 | string contents; |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 188 | if (!utils::ReadFile(marker_path.value(), &contents)) |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 189 | 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 Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 196 | bool HardwareChromeOS::SchedulePowerwash() { |
| 197 | bool result = utils::WriteFile( |
| 198 | kPowerwashMarkerFile, kPowerwashCommand, strlen(kPowerwashCommand)); |
| 199 | if (result) { |
Alex Deymo | caa4672 | 2016-06-09 12:08:29 -0700 | [diff] [blame] | 200 | LOG(INFO) << "Created " << kPowerwashMarkerFile |
| 201 | << " to powerwash on next reboot"; |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 202 | } else { |
Alex Deymo | caa4672 | 2016-06-09 12:08:29 -0700 | [diff] [blame] | 203 | PLOG(ERROR) << "Error in creating powerwash marker file: " |
| 204 | << kPowerwashMarkerFile; |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | return result; |
| 208 | } |
| 209 | |
| 210 | bool 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 Deymo | caa4672 | 2016-06-09 12:08:29 -0700 | [diff] [blame] | 215 | << kPowerwashMarkerFile; |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 216 | } else { |
| 217 | PLOG(ERROR) << "Could not delete the powerwash marker file : " |
Alex Deymo | caa4672 | 2016-06-09 12:08:29 -0700 | [diff] [blame] | 218 | << kPowerwashMarkerFile; |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | return result; |
| 222 | } |
| 223 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 224 | bool HardwareChromeOS::GetNonVolatileDirectory(base::FilePath* path) const { |
Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 225 | *path = base::FilePath(constants::kNonVolatileDirectory); |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 226 | return true; |
| 227 | } |
| 228 | |
| 229 | bool HardwareChromeOS::GetPowerwashSafeDirectory(base::FilePath* path) const { |
| 230 | *path = base::FilePath(kPowerwashSafeDirectory); |
| 231 | return true; |
| 232 | } |
| 233 | |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 234 | void 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 Hassani | 1677e81 | 2017-06-21 13:36:36 -0700 | [diff] [blame] | 252 | bool 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 | |
| 275 | void 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 Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 297 | } // namespace chromeos_update_engine |