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> |
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" |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 38 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 39 | using std::string; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 40 | using std::vector; |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 41 | |
Alex Deymo | bccbc38 | 2014-04-03 13:38:55 -0700 | [diff] [blame] | 42 | namespace { |
| 43 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 44 | const char kOOBECompletedMarker[] = "/home/chronos/.oobe_completed"; |
| 45 | |
| 46 | // The stateful directory used by update_engine to store powerwash-safe files. |
| 47 | // The files stored here must be whitelisted in the powerwash scripts. |
| 48 | const char kPowerwashSafeDirectory[] = |
| 49 | "/mnt/stateful_partition/unencrypted/preserve"; |
Alex Deymo | bccbc38 | 2014-04-03 13:38:55 -0700 | [diff] [blame] | 50 | |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 51 | // The powerwash_count marker file contains the number of times the device was |
| 52 | // powerwashed. This value is incremented by the clobber-state script when |
| 53 | // a powerwash is performed. |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 54 | const char kPowerwashCountMarker[] = "powerwash_count"; |
| 55 | |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 56 | // UpdateManager config path. |
| 57 | const char* kConfigFilePath = "/etc/update_manager.conf"; |
| 58 | |
| 59 | // UpdateManager config options: |
| 60 | const char* kConfigOptsIsOOBEEnabled = "is_oobe_enabled"; |
| 61 | |
Alex Deymo | bccbc38 | 2014-04-03 13:38:55 -0700 | [diff] [blame] | 62 | } // namespace |
| 63 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 64 | namespace chromeos_update_engine { |
| 65 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 66 | namespace hardware { |
| 67 | |
| 68 | // Factory defined in hardware.h. |
| 69 | std::unique_ptr<HardwareInterface> CreateHardware() { |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 70 | std::unique_ptr<HardwareChromeOS> hardware(new HardwareChromeOS()); |
| 71 | hardware->Init(); |
| 72 | return std::move(hardware); |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | } // namespace hardware |
| 76 | |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 77 | void HardwareChromeOS::Init() { |
| 78 | LoadConfig("" /* root_prefix */, IsNormalBootMode()); |
| 79 | } |
| 80 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 81 | bool HardwareChromeOS::IsOfficialBuild() const { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 82 | return VbGetSystemPropertyInt("debug_build") == 0; |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 85 | bool HardwareChromeOS::IsNormalBootMode() const { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 86 | bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0; |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 87 | return !dev_mode; |
| 88 | } |
| 89 | |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 90 | bool HardwareChromeOS::IsOOBEEnabled() const { |
| 91 | return is_oobe_enabled_; |
| 92 | } |
| 93 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 94 | bool HardwareChromeOS::IsOOBEComplete(base::Time* out_time_of_oobe) const { |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 95 | if (!is_oobe_enabled_) { |
| 96 | LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() was called"; |
| 97 | } |
Alex Deymo | bccbc38 | 2014-04-03 13:38:55 -0700 | [diff] [blame] | 98 | struct stat statbuf; |
| 99 | if (stat(kOOBECompletedMarker, &statbuf) != 0) { |
| 100 | if (errno != ENOENT) { |
| 101 | PLOG(ERROR) << "Error getting information about " |
| 102 | << kOOBECompletedMarker; |
| 103 | } |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | if (out_time_of_oobe != nullptr) |
| 108 | *out_time_of_oobe = base::Time::FromTimeT(statbuf.st_mtime); |
| 109 | return true; |
| 110 | } |
| 111 | |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 112 | static string ReadValueFromCrosSystem(const string& key) { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 113 | char value_buffer[VB_MAX_STRING_PROPERTY]; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 114 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 115 | const char* rv = VbGetSystemPropertyString(key.c_str(), value_buffer, |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 116 | sizeof(value_buffer)); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 117 | if (rv != nullptr) { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 118 | string return_value(value_buffer); |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 119 | base::TrimWhitespaceASCII(return_value, base::TRIM_ALL, &return_value); |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 120 | return return_value; |
| 121 | } |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 122 | |
| 123 | LOG(ERROR) << "Unable to read crossystem key " << key; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 124 | return ""; |
| 125 | } |
| 126 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 127 | string HardwareChromeOS::GetHardwareClass() const { |
Chris Masone | f8d037f | 2014-02-19 01:53:00 +0000 | [diff] [blame] | 128 | if (USE_HWID_OVERRIDE) { |
| 129 | return HwidOverride::Read(base::FilePath("/")); |
| 130 | } |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 131 | return ReadValueFromCrosSystem("hwid"); |
| 132 | } |
| 133 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 134 | string HardwareChromeOS::GetFirmwareVersion() const { |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 135 | return ReadValueFromCrosSystem("fwid"); |
| 136 | } |
| 137 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 138 | string HardwareChromeOS::GetECVersion() const { |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 139 | string input_line; |
| 140 | int exit_code = 0; |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 141 | vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"}; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 142 | |
| 143 | bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line); |
| 144 | if (!success || exit_code) { |
| 145 | LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")"; |
| 146 | return ""; |
| 147 | } |
| 148 | |
| 149 | return utils::ParseECVersion(input_line); |
| 150 | } |
| 151 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 152 | int HardwareChromeOS::GetPowerwashCount() const { |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 153 | int powerwash_count; |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 154 | base::FilePath marker_path = base::FilePath(kPowerwashSafeDirectory).Append( |
| 155 | kPowerwashCountMarker); |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 156 | string contents; |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 157 | if (!utils::ReadFile(marker_path.value(), &contents)) |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 158 | return -1; |
| 159 | base::TrimWhitespaceASCII(contents, base::TRIM_TRAILING, &contents); |
| 160 | if (!base::StringToInt(contents, &powerwash_count)) |
| 161 | return -1; |
| 162 | return powerwash_count; |
| 163 | } |
| 164 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 165 | bool HardwareChromeOS::GetNonVolatileDirectory(base::FilePath* path) const { |
Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 166 | *path = base::FilePath(constants::kNonVolatileDirectory); |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 167 | return true; |
| 168 | } |
| 169 | |
| 170 | bool HardwareChromeOS::GetPowerwashSafeDirectory(base::FilePath* path) const { |
| 171 | *path = base::FilePath(kPowerwashSafeDirectory); |
| 172 | return true; |
| 173 | } |
| 174 | |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 175 | void HardwareChromeOS::LoadConfig(const string& root_prefix, bool normal_mode) { |
| 176 | brillo::KeyValueStore store; |
| 177 | |
| 178 | if (normal_mode) { |
| 179 | store.Load(base::FilePath(root_prefix + kConfigFilePath)); |
| 180 | } else { |
| 181 | if (store.Load(base::FilePath(root_prefix + kStatefulPartition + |
| 182 | kConfigFilePath))) { |
| 183 | LOG(INFO) << "UpdateManager Config loaded from stateful partition."; |
| 184 | } else { |
| 185 | store.Load(base::FilePath(root_prefix + kConfigFilePath)); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | if (!store.GetBoolean(kConfigOptsIsOOBEEnabled, &is_oobe_enabled_)) |
| 190 | is_oobe_enabled_ = true; // Default value. |
| 191 | } |
| 192 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 193 | } // namespace chromeos_update_engine |