blob: 85131fc4c8dff5bcccd323837271866bf28da7ff [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 Deymo1b03f9f2015-12-09 00:38:36 -080017#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 Vakulenko3f39d5c2015-10-13 09:27:13 -070023#include <brillo/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 Deymo39910dc2015-11-09 17:04:30 -080030#include "update_engine/common/hardware.h"
31#include "update_engine/common/hwid_override.h"
Sen Jiang9c123462015-11-19 13:16:23 -080032#include "update_engine/common/platform_constants.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080033#include "update_engine/common/subprocess.h"
34#include "update_engine/common/utils.h"
J. Richard Barnette522d36f2013-10-28 17:22:12 -070035
Alex Deymo42432912013-07-12 20:21:15 -070036using std::string;
J. Richard Barnette522d36f2013-10-28 17:22:12 -070037using std::vector;
Alex Deymo42432912013-07-12 20:21:15 -070038
Alex Deymobccbc382014-04-03 13:38:55 -070039namespace {
40
Alex Deymodd132f32015-09-14 19:12:07 -070041const char kOOBECompletedMarker[] = "/home/chronos/.oobe_completed";
42
43// The stateful directory used by update_engine to store powerwash-safe files.
44// The files stored here must be whitelisted in the powerwash scripts.
45const char kPowerwashSafeDirectory[] =
46 "/mnt/stateful_partition/unencrypted/preserve";
Alex Deymobccbc382014-04-03 13:38:55 -070047
Alex Deymoebbe7ef2014-10-30 13:02:49 -070048// The powerwash_count marker file contains the number of times the device was
49// powerwashed. This value is incremented by the clobber-state script when
50// a powerwash is performed.
Alex Deymodd132f32015-09-14 19:12:07 -070051const char kPowerwashCountMarker[] = "powerwash_count";
52
Alex Deymoa918f9d2016-06-03 19:26:58 -070053// UpdateManager config path.
54const char* kConfigFilePath = "/etc/update_manager.conf";
55
56// UpdateManager config options:
57const char* kConfigOptsIsOOBEEnabled = "is_oobe_enabled";
58
Alex Deymobccbc382014-04-03 13:38:55 -070059} // namespace
60
Alex Deymo42432912013-07-12 20:21:15 -070061namespace chromeos_update_engine {
62
Alex Deymo40d86b22015-09-03 22:27:10 -070063namespace hardware {
64
65// Factory defined in hardware.h.
66std::unique_ptr<HardwareInterface> CreateHardware() {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070067 return brillo::make_unique_ptr(new HardwareChromeOS());
Alex Deymo40d86b22015-09-03 22:27:10 -070068}
69
70} // namespace hardware
71
72bool HardwareChromeOS::IsOfficialBuild() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070073 return VbGetSystemPropertyInt("debug_build") == 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -070074}
75
Alex Deymo40d86b22015-09-03 22:27:10 -070076bool HardwareChromeOS::IsNormalBootMode() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070077 bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -070078 return !dev_mode;
79}
80
Alex Deymo40d86b22015-09-03 22:27:10 -070081bool HardwareChromeOS::IsOOBEComplete(base::Time* out_time_of_oobe) const {
Alex Deymobccbc382014-04-03 13:38:55 -070082 struct stat statbuf;
83 if (stat(kOOBECompletedMarker, &statbuf) != 0) {
84 if (errno != ENOENT) {
85 PLOG(ERROR) << "Error getting information about "
86 << kOOBECompletedMarker;
87 }
88 return false;
89 }
90
91 if (out_time_of_oobe != nullptr)
92 *out_time_of_oobe = base::Time::FromTimeT(statbuf.st_mtime);
93 return true;
94}
95
J. Richard Barnette522d36f2013-10-28 17:22:12 -070096static string ReadValueFromCrosSystem(const string& key) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070097 char value_buffer[VB_MAX_STRING_PROPERTY];
J. Richard Barnette522d36f2013-10-28 17:22:12 -070098
Alex Deymo40d86b22015-09-03 22:27:10 -070099 const char* rv = VbGetSystemPropertyString(key.c_str(), value_buffer,
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700100 sizeof(value_buffer));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700101 if (rv != nullptr) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700102 string return_value(value_buffer);
Ben Chan736fcb52014-05-21 18:28:22 -0700103 base::TrimWhitespaceASCII(return_value, base::TRIM_ALL, &return_value);
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700104 return return_value;
105 }
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700106
107 LOG(ERROR) << "Unable to read crossystem key " << key;
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700108 return "";
109}
110
Alex Deymo40d86b22015-09-03 22:27:10 -0700111string HardwareChromeOS::GetHardwareClass() const {
Chris Masonef8d037f2014-02-19 01:53:00 +0000112 if (USE_HWID_OVERRIDE) {
113 return HwidOverride::Read(base::FilePath("/"));
114 }
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700115 return ReadValueFromCrosSystem("hwid");
116}
117
Alex Deymo40d86b22015-09-03 22:27:10 -0700118string HardwareChromeOS::GetFirmwareVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700119 return ReadValueFromCrosSystem("fwid");
120}
121
Alex Deymo40d86b22015-09-03 22:27:10 -0700122string HardwareChromeOS::GetECVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700123 string input_line;
124 int exit_code = 0;
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800125 vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"};
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700126
127 bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line);
128 if (!success || exit_code) {
129 LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")";
130 return "";
131 }
132
133 return utils::ParseECVersion(input_line);
134}
135
Alex Deymo40d86b22015-09-03 22:27:10 -0700136int HardwareChromeOS::GetPowerwashCount() const {
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700137 int powerwash_count;
Alex Deymodd132f32015-09-14 19:12:07 -0700138 base::FilePath marker_path = base::FilePath(kPowerwashSafeDirectory).Append(
139 kPowerwashCountMarker);
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700140 string contents;
Alex Deymodd132f32015-09-14 19:12:07 -0700141 if (!utils::ReadFile(marker_path.value(), &contents))
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700142 return -1;
143 base::TrimWhitespaceASCII(contents, base::TRIM_TRAILING, &contents);
144 if (!base::StringToInt(contents, &powerwash_count))
145 return -1;
146 return powerwash_count;
147}
148
Alex Deymoa918f9d2016-06-03 19:26:58 -0700149bool HardwareChromeOS::SchedulePowerwash() {
150 bool result = utils::WriteFile(
151 kPowerwashMarkerFile, kPowerwashCommand, strlen(kPowerwashCommand));
152 if (result) {
153 LOG(INFO) << "Created " << marker_file << " to powerwash on next reboot";
154 } else {
155 PLOG(ERROR) << "Error in creating powerwash marker file: " << marker_file;
156 }
157
158 return result;
159}
160
161bool HardwareChromeOS::CancelPowerwash() {
162 bool result = base::DeleteFile(base::FilePath(kPowerwashMarkerFile), false);
163
164 if (result) {
165 LOG(INFO) << "Successfully deleted the powerwash marker file : "
166 << marker_file;
167 } else {
168 PLOG(ERROR) << "Could not delete the powerwash marker file : "
169 << marker_file;
170 }
171
172 return result;
173}
174
Alex Deymodd132f32015-09-14 19:12:07 -0700175bool HardwareChromeOS::GetNonVolatileDirectory(base::FilePath* path) const {
Sen Jiang9c123462015-11-19 13:16:23 -0800176 *path = base::FilePath(constants::kNonVolatileDirectory);
Alex Deymodd132f32015-09-14 19:12:07 -0700177 return true;
178}
179
180bool HardwareChromeOS::GetPowerwashSafeDirectory(base::FilePath* path) const {
181 *path = base::FilePath(kPowerwashSafeDirectory);
182 return true;
183}
184
Alex Deymo42432912013-07-12 20:21:15 -0700185} // namespace chromeos_update_engine