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 | |
| 17 | #include "update_engine/hardware.h" |
| 18 | |
Ben Chan | 06c76a4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 19 | #include <base/files/file_util.h> |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 20 | #include <base/logging.h> |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 21 | #include <base/strings/string_number_conversions.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 22 | #include <base/strings/string_util.h> |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 23 | #include <vboot/crossystem.h> |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 24 | |
Don Garrett | 83692e4 | 2013-11-08 10:11:30 -0800 | [diff] [blame] | 25 | extern "C" { |
| 26 | #include "vboot/vboot_host.h" |
| 27 | } |
| 28 | |
Chris Masone | f8d037f | 2014-02-19 01:53:00 +0000 | [diff] [blame] | 29 | #include "update_engine/hwid_override.h" |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 30 | #include "update_engine/subprocess.h" |
| 31 | #include "update_engine/utils.h" |
| 32 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 33 | using std::string; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 34 | using std::vector; |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 35 | |
Alex Deymo | bccbc38 | 2014-04-03 13:38:55 -0700 | [diff] [blame] | 36 | namespace { |
| 37 | |
| 38 | static const char kOOBECompletedMarker[] = "/home/chronos/.oobe_completed"; |
| 39 | |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 40 | // The powerwash_count marker file contains the number of times the device was |
| 41 | // powerwashed. This value is incremented by the clobber-state script when |
| 42 | // a powerwash is performed. |
| 43 | static const char kPowerwashCountMarker[] = |
| 44 | "/mnt/stateful_partition/unencrypted/preserve/powerwash_count"; |
| 45 | |
Alex Deymo | bccbc38 | 2014-04-03 13:38:55 -0700 | [diff] [blame] | 46 | } // namespace |
| 47 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 48 | namespace chromeos_update_engine { |
| 49 | |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 50 | bool Hardware::IsOfficialBuild() const { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 51 | return VbGetSystemPropertyInt("debug_build") == 0; |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 54 | bool Hardware::IsNormalBootMode() const { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 55 | bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0; |
J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 56 | LOG_IF(INFO, dev_mode) << "Booted in dev mode."; |
| 57 | return !dev_mode; |
| 58 | } |
| 59 | |
Alex Deymo | bccbc38 | 2014-04-03 13:38:55 -0700 | [diff] [blame] | 60 | bool Hardware::IsOOBEComplete(base::Time* out_time_of_oobe) const { |
| 61 | struct stat statbuf; |
| 62 | if (stat(kOOBECompletedMarker, &statbuf) != 0) { |
| 63 | if (errno != ENOENT) { |
| 64 | PLOG(ERROR) << "Error getting information about " |
| 65 | << kOOBECompletedMarker; |
| 66 | } |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | if (out_time_of_oobe != nullptr) |
| 71 | *out_time_of_oobe = base::Time::FromTimeT(statbuf.st_mtime); |
| 72 | return true; |
| 73 | } |
| 74 | |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 75 | static string ReadValueFromCrosSystem(const string& key) { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 76 | char value_buffer[VB_MAX_STRING_PROPERTY]; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 77 | |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 78 | const char *rv = VbGetSystemPropertyString(key.c_str(), value_buffer, |
| 79 | sizeof(value_buffer)); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 80 | if (rv != nullptr) { |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 81 | string return_value(value_buffer); |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 82 | base::TrimWhitespaceASCII(return_value, base::TRIM_ALL, &return_value); |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 83 | return return_value; |
| 84 | } |
J. Richard Barnette | c7dd853 | 2013-10-29 16:30:46 -0700 | [diff] [blame] | 85 | |
| 86 | LOG(ERROR) << "Unable to read crossystem key " << key; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 87 | return ""; |
| 88 | } |
| 89 | |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 90 | string Hardware::GetHardwareClass() const { |
Chris Masone | f8d037f | 2014-02-19 01:53:00 +0000 | [diff] [blame] | 91 | if (USE_HWID_OVERRIDE) { |
| 92 | return HwidOverride::Read(base::FilePath("/")); |
| 93 | } |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 94 | return ReadValueFromCrosSystem("hwid"); |
| 95 | } |
| 96 | |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 97 | string Hardware::GetFirmwareVersion() const { |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 98 | return ReadValueFromCrosSystem("fwid"); |
| 99 | } |
| 100 | |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 101 | string Hardware::GetECVersion() const { |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 102 | string input_line; |
| 103 | int exit_code = 0; |
Alex Vakulenko | d0fdfb3 | 2014-02-21 15:26:26 -0800 | [diff] [blame] | 104 | vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"}; |
J. Richard Barnette | 522d36f | 2013-10-28 17:22:12 -0700 | [diff] [blame] | 105 | |
| 106 | bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line); |
| 107 | if (!success || exit_code) { |
| 108 | LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")"; |
| 109 | return ""; |
| 110 | } |
| 111 | |
| 112 | return utils::ParseECVersion(input_line); |
| 113 | } |
| 114 | |
Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 115 | int Hardware::GetPowerwashCount() const { |
| 116 | int powerwash_count; |
| 117 | string contents; |
| 118 | if (!utils::ReadFile(kPowerwashCountMarker, &contents)) |
| 119 | return -1; |
| 120 | base::TrimWhitespaceASCII(contents, base::TRIM_TRAILING, &contents); |
| 121 | if (!base::StringToInt(contents, &powerwash_count)) |
| 122 | return -1; |
| 123 | return powerwash_count; |
| 124 | } |
| 125 | |
Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 126 | } // namespace chromeos_update_engine |