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