blob: 9b3a05dc1f29ae2ebf0c7c6207f0c9535b8f7ed2 [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
Alex Deymo46a9aae2016-05-04 20:20:11 -070019#include <base/files/file_path.h>
Ben Chan06c76a42014-09-05 08:21:06 -070020#include <base/files/file_util.h>
Alex Deymo42432912013-07-12 20:21:15 -070021#include <base/logging.h>
Alex Deymoebbe7ef2014-10-30 13:02:49 -070022#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070023#include <base/strings/string_util.h>
Alex Deymo46a9aae2016-05-04 20:20:11 -070024#include <brillo/key_value_store.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070025#include <brillo/make_unique_ptr.h>
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070026#include <vboot/crossystem.h>
Alex Deymo42432912013-07-12 20:21:15 -070027
Don Garrett83692e42013-11-08 10:11:30 -080028extern "C" {
29#include "vboot/vboot_host.h"
30}
31
Alex Deymo46a9aae2016-05-04 20:20:11 -070032#include "update_engine/common/constants.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080033#include "update_engine/common/hardware.h"
34#include "update_engine/common/hwid_override.h"
Sen Jiang9c123462015-11-19 13:16:23 -080035#include "update_engine/common/platform_constants.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080036#include "update_engine/common/subprocess.h"
37#include "update_engine/common/utils.h"
J. Richard Barnette522d36f2013-10-28 17:22:12 -070038
Alex Deymo42432912013-07-12 20:21:15 -070039using std::string;
J. Richard Barnette522d36f2013-10-28 17:22:12 -070040using std::vector;
Alex Deymo42432912013-07-12 20:21:15 -070041
Alex Deymobccbc382014-04-03 13:38:55 -070042namespace {
43
Alex Deymodd132f32015-09-14 19:12:07 -070044const 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.
48const char kPowerwashSafeDirectory[] =
49 "/mnt/stateful_partition/unencrypted/preserve";
Alex Deymobccbc382014-04-03 13:38:55 -070050
Alex Deymoebbe7ef2014-10-30 13:02:49 -070051// 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 Deymodd132f32015-09-14 19:12:07 -070054const char kPowerwashCountMarker[] = "powerwash_count";
55
Alex Deymofb905d92016-06-03 19:26:58 -070056// The name of the marker file used to trigger powerwash when post-install
57// completes successfully so that the device is powerwashed on next reboot.
58const char kPowerwashMarkerFile[] =
59 "/mnt/stateful_partition/factory_install_reset";
60
61// The contents of the powerwash marker file.
62const char kPowerwashCommand[] = "safe fast keepimg reason=update_engine\n";
63
Alex Deymo46a9aae2016-05-04 20:20:11 -070064// UpdateManager config path.
65const char* kConfigFilePath = "/etc/update_manager.conf";
66
67// UpdateManager config options:
68const char* kConfigOptsIsOOBEEnabled = "is_oobe_enabled";
69
Alex Deymobccbc382014-04-03 13:38:55 -070070} // namespace
71
Alex Deymo42432912013-07-12 20:21:15 -070072namespace chromeos_update_engine {
73
Alex Deymo40d86b22015-09-03 22:27:10 -070074namespace hardware {
75
76// Factory defined in hardware.h.
77std::unique_ptr<HardwareInterface> CreateHardware() {
Alex Deymo46a9aae2016-05-04 20:20:11 -070078 std::unique_ptr<HardwareChromeOS> hardware(new HardwareChromeOS());
79 hardware->Init();
80 return std::move(hardware);
Alex Deymo40d86b22015-09-03 22:27:10 -070081}
82
83} // namespace hardware
84
Alex Deymo46a9aae2016-05-04 20:20:11 -070085void HardwareChromeOS::Init() {
86 LoadConfig("" /* root_prefix */, IsNormalBootMode());
87}
88
Alex Deymo40d86b22015-09-03 22:27:10 -070089bool HardwareChromeOS::IsOfficialBuild() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070090 return VbGetSystemPropertyInt("debug_build") == 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -070091}
92
Alex Deymo40d86b22015-09-03 22:27:10 -070093bool HardwareChromeOS::IsNormalBootMode() const {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -070094 bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
J. Richard Barnette056b0ab2013-10-29 15:24:56 -070095 return !dev_mode;
96}
97
Alex Deymo46a9aae2016-05-04 20:20:11 -070098bool HardwareChromeOS::IsOOBEEnabled() const {
99 return is_oobe_enabled_;
100}
101
Alex Deymo40d86b22015-09-03 22:27:10 -0700102bool HardwareChromeOS::IsOOBEComplete(base::Time* out_time_of_oobe) const {
Alex Deymo46a9aae2016-05-04 20:20:11 -0700103 if (!is_oobe_enabled_) {
104 LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() was called";
105 }
Alex Deymobccbc382014-04-03 13:38:55 -0700106 struct stat statbuf;
107 if (stat(kOOBECompletedMarker, &statbuf) != 0) {
108 if (errno != ENOENT) {
109 PLOG(ERROR) << "Error getting information about "
110 << kOOBECompletedMarker;
111 }
112 return false;
113 }
114
115 if (out_time_of_oobe != nullptr)
116 *out_time_of_oobe = base::Time::FromTimeT(statbuf.st_mtime);
117 return true;
118}
119
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700120static string ReadValueFromCrosSystem(const string& key) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700121 char value_buffer[VB_MAX_STRING_PROPERTY];
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700122
Alex Deymo40d86b22015-09-03 22:27:10 -0700123 const char* rv = VbGetSystemPropertyString(key.c_str(), value_buffer,
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700124 sizeof(value_buffer));
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700125 if (rv != nullptr) {
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700126 string return_value(value_buffer);
Ben Chan736fcb52014-05-21 18:28:22 -0700127 base::TrimWhitespaceASCII(return_value, base::TRIM_ALL, &return_value);
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700128 return return_value;
129 }
J. Richard Barnettec7dd8532013-10-29 16:30:46 -0700130
131 LOG(ERROR) << "Unable to read crossystem key " << key;
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700132 return "";
133}
134
Alex Deymo40d86b22015-09-03 22:27:10 -0700135string HardwareChromeOS::GetHardwareClass() const {
Chris Masonef8d037f2014-02-19 01:53:00 +0000136 if (USE_HWID_OVERRIDE) {
137 return HwidOverride::Read(base::FilePath("/"));
138 }
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700139 return ReadValueFromCrosSystem("hwid");
140}
141
Alex Deymo40d86b22015-09-03 22:27:10 -0700142string HardwareChromeOS::GetFirmwareVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700143 return ReadValueFromCrosSystem("fwid");
144}
145
Alex Deymo40d86b22015-09-03 22:27:10 -0700146string HardwareChromeOS::GetECVersion() const {
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700147 string input_line;
148 int exit_code = 0;
Alex Vakulenkod0fdfb32014-02-21 15:26:26 -0800149 vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"};
J. Richard Barnette522d36f2013-10-28 17:22:12 -0700150
151 bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line);
152 if (!success || exit_code) {
153 LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")";
154 return "";
155 }
156
157 return utils::ParseECVersion(input_line);
158}
159
Alex Deymo40d86b22015-09-03 22:27:10 -0700160int HardwareChromeOS::GetPowerwashCount() const {
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700161 int powerwash_count;
Alex Deymodd132f32015-09-14 19:12:07 -0700162 base::FilePath marker_path = base::FilePath(kPowerwashSafeDirectory).Append(
163 kPowerwashCountMarker);
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700164 string contents;
Alex Deymodd132f32015-09-14 19:12:07 -0700165 if (!utils::ReadFile(marker_path.value(), &contents))
Alex Deymoebbe7ef2014-10-30 13:02:49 -0700166 return -1;
167 base::TrimWhitespaceASCII(contents, base::TRIM_TRAILING, &contents);
168 if (!base::StringToInt(contents, &powerwash_count))
169 return -1;
170 return powerwash_count;
171}
172
Alex Deymofb905d92016-06-03 19:26:58 -0700173bool HardwareChromeOS::SchedulePowerwash() {
174 bool result = utils::WriteFile(
175 kPowerwashMarkerFile, kPowerwashCommand, strlen(kPowerwashCommand));
176 if (result) {
177 LOG(INFO) << "Created " << marker_file << " to powerwash on next reboot";
178 } else {
179 PLOG(ERROR) << "Error in creating powerwash marker file: " << marker_file;
180 }
181
182 return result;
183}
184
185bool HardwareChromeOS::CancelPowerwash() {
186 bool result = base::DeleteFile(base::FilePath(kPowerwashMarkerFile), false);
187
188 if (result) {
189 LOG(INFO) << "Successfully deleted the powerwash marker file : "
190 << marker_file;
191 } else {
192 PLOG(ERROR) << "Could not delete the powerwash marker file : "
193 << marker_file;
194 }
195
196 return result;
197}
198
Alex Deymodd132f32015-09-14 19:12:07 -0700199bool HardwareChromeOS::GetNonVolatileDirectory(base::FilePath* path) const {
Sen Jiang9c123462015-11-19 13:16:23 -0800200 *path = base::FilePath(constants::kNonVolatileDirectory);
Alex Deymodd132f32015-09-14 19:12:07 -0700201 return true;
202}
203
204bool HardwareChromeOS::GetPowerwashSafeDirectory(base::FilePath* path) const {
205 *path = base::FilePath(kPowerwashSafeDirectory);
206 return true;
207}
208
Alex Deymo46a9aae2016-05-04 20:20:11 -0700209void HardwareChromeOS::LoadConfig(const string& root_prefix, bool normal_mode) {
210 brillo::KeyValueStore store;
211
212 if (normal_mode) {
213 store.Load(base::FilePath(root_prefix + kConfigFilePath));
214 } else {
215 if (store.Load(base::FilePath(root_prefix + kStatefulPartition +
216 kConfigFilePath))) {
217 LOG(INFO) << "UpdateManager Config loaded from stateful partition.";
218 } else {
219 store.Load(base::FilePath(root_prefix + kConfigFilePath));
220 }
221 }
222
223 if (!store.GetBoolean(kConfigOptsIsOOBEEnabled, &is_oobe_enabled_))
224 is_oobe_enabled_ = true; // Default value.
225}
226
Alex Deymo42432912013-07-12 20:21:15 -0700227} // namespace chromeos_update_engine