blob: 888772d5e34fa9d8a622cd70f0945a01f1323bcc [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 Deymo40d86b22015-09-03 22:27:10 -070017#include "update_engine/hardware_chromeos.h"
Alex Deymo42432912013-07-12 20:21:15 -070018
Ben Chan06c76a42014-09-05 08:21:06 -070019#include <base/files/file_util.h>
Alex Deymo42432912013-07-12 20:21:15 -070020#include <base/logging.h>
Alex Deymoebbe7ef2014-10-30 13:02:49 -070021#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070022#include <base/strings/string_util.h>
Alex Deymo40d86b22015-09-03 22:27:10 -070023#include <chromeos/make_unique_ptr.h>
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070024#include <vboot/crossystem.h>
Alex Deymo42432912013-07-12 20:21:15 -070025
Don Garrett83692e42013-11-08 10:11:30 -080026extern "C" {
27#include "vboot/vboot_host.h"
28}
29
Alex Deymo40d86b22015-09-03 22:27:10 -070030#include "update_engine/hardware.h"
Chris Masonef8d037f2014-02-19 01:53:00 +000031#include "update_engine/hwid_override.h"
J. Richard Barnette522d36f2013-10-28 17:22:12 -070032#include "update_engine/subprocess.h"
33#include "update_engine/utils.h"
34
Alex Deymo42432912013-07-12 20:21:15 -070035using std::string;
J. Richard Barnette522d36f2013-10-28 17:22:12 -070036using std::vector;
Alex Deymo42432912013-07-12 20:21:15 -070037
Alex Deymobccbc382014-04-03 13:38:55 -070038namespace {
39
40static const char kOOBECompletedMarker[] = "/home/chronos/.oobe_completed";
41
Alex Deymoebbe7ef2014-10-30 13:02:49 -070042// The powerwash_count marker file contains the number of times the device was
43// powerwashed. This value is incremented by the clobber-state script when
44// a powerwash is performed.
45static const char kPowerwashCountMarker[] =
46 "/mnt/stateful_partition/unencrypted/preserve/powerwash_count";
47
Alex Deymobccbc382014-04-03 13:38:55 -070048} // namespace
49
Alex Deymo42432912013-07-12 20:21:15 -070050namespace chromeos_update_engine {
51
Alex Deymo40d86b22015-09-03 22:27:10 -070052namespace hardware {
53
54// Factory defined in hardware.h.
55std::unique_ptr<HardwareInterface> CreateHardware() {
56 return chromeos::make_unique_ptr(new HardwareChromeOS());
57}
58
59} // namespace hardware
60
61bool HardwareChromeOS::IsOfficialBuild() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070062 return VbGetSystemPropertyInt("debug_build") == 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -070063}
64
Alex Deymo40d86b22015-09-03 22:27:10 -070065bool HardwareChromeOS::IsNormalBootMode() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070066 bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -070067 LOG_IF(INFO, dev_mode) << "Booted in dev mode.";
68 return !dev_mode;
69}
70
Alex Deymo40d86b22015-09-03 22:27:10 -070071bool HardwareChromeOS::IsOOBEComplete(base::Time* out_time_of_oobe) const {
Alex Deymobccbc382014-04-03 13:38:55 -070072 struct stat statbuf;
73 if (stat(kOOBECompletedMarker, &statbuf) != 0) {
74 if (errno != ENOENT) {
75 PLOG(ERROR) << "Error getting information about "
76 << kOOBECompletedMarker;
77 }
78 return false;
79 }
80
81 if (out_time_of_oobe != nullptr)
82 *out_time_of_oobe = base::Time::FromTimeT(statbuf.st_mtime);
83 return true;
84}
85
J. Richard Barnette522d36f2013-10-28 17:22:12 -070086static string ReadValueFromCrosSystem(const string& key) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070087 char value_buffer[VB_MAX_STRING_PROPERTY];
J. Richard Barnette522d36f2013-10-28 17:22:12 -070088
Alex Deymo40d86b22015-09-03 22:27:10 -070089 const char* rv = VbGetSystemPropertyString(key.c_str(), value_buffer,
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070090 sizeof(value_buffer));
Alex Vakulenko88b591f2014-08-28 16:48:57 -070091 if (rv != nullptr) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070092 string return_value(value_buffer);
Ben Chan736fcb52014-05-21 18:28:22 -070093 base::TrimWhitespaceASCII(return_value, base::TRIM_ALL, &return_value);
J. Richard Barnette522d36f2013-10-28 17:22:12 -070094 return return_value;
95 }
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070096
97 LOG(ERROR) << "Unable to read crossystem key " << key;
J. Richard Barnette522d36f2013-10-28 17:22:12 -070098 return "";
99}
100
Alex Deymo40d86b22015-09-03 22:27:10 -0700101string HardwareChromeOS::GetHardwareClass() const {
Chris Masonef8d037f2014-02-19 01:53:00 +0000102 if (USE_HWID_OVERRIDE) {
103 return HwidOverride::Read(base::FilePath("/"));
104 }
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700105 return ReadValueFromCrosSystem("hwid");
106}
107
Alex Deymo40d86b22015-09-03 22:27:10 -0700108string HardwareChromeOS::GetFirmwareVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700109 return ReadValueFromCrosSystem("fwid");
110}
111
Alex Deymo40d86b22015-09-03 22:27:10 -0700112string HardwareChromeOS::GetECVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700113 string input_line;
114 int exit_code = 0;
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800115 vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"};
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700116
117 bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line);
118 if (!success || exit_code) {
119 LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")";
120 return "";
121 }
122
123 return utils::ParseECVersion(input_line);
124}
125
Alex Deymo40d86b22015-09-03 22:27:10 -0700126int HardwareChromeOS::GetPowerwashCount() const {
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700127 int powerwash_count;
128 string contents;
129 if (!utils::ReadFile(kPowerwashCountMarker, &contents))
130 return -1;
131 base::TrimWhitespaceASCII(contents, base::TRIM_TRAILING, &contents);
132 if (!base::StringToInt(contents, &powerwash_count))
133 return -1;
134 return powerwash_count;
135}
136
Alex Deymo42432912013-07-12 20:21:15 -0700137} // namespace chromeos_update_engine